C# 当我调用一个方法时,为什么不是';是否调用了被重写的方法?
我的问题是,我的方法调用函数指向虚拟方法,而不是被重写的方法。我试图用虚拟方法继承这个类,当我调试它时,它没有什么不同。少了什么C# 当我调用一个方法时,为什么不是';是否调用了被重写的方法?,c#,visual-studio,class,oop,C#,Visual Studio,Class,Oop,我的问题是,我的方法调用函数指向虚拟方法,而不是被重写的方法。我试图用虚拟方法继承这个类,当我调试它时,它没有什么不同。少了什么 public class Engine { protected virtual void ExecuteCommand(string[] inputParams) { switch (inputParams[0]) { case "status": this.Pri
public class Engine
{
protected virtual void ExecuteCommand(string[] inputParams)
{
switch (inputParams[0])
{
case "status":
this.PrintCharactersStatus(this.characterList);
break;
}
}
protected virtual void CreateCharacter(string[] inputParams)
{
}
protected virtual void AddItem(string[] inputParams)
{
}
private void ReadUserInput()
{
string inputLine = Console.ReadLine();
while (inputLine != string.Empty)
{
string[] parameters = inputLine
.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
ExecuteCommand(parameters);
inputLine = Console.ReadLine();
}
}
}
public class Program : Engine
{
public static void Main()
{
Engine engine = new Engine();
engine.Run();
}
protected override void ExecuteCommand(string[] inputParams)
{
base.ExecuteCommand(inputParams);
switch (inputParams[0])
{
case "create":
this.CreateCharacter(inputParams);
break;
case "add":
this.AddItem(inputParams);
break;
}
}
您正在创建的是
引擎
的实例,而不是程序
——您只需将主
的第一行更改为:
Engine engine = new Program();
要使用的实现基于调用方法的对象的执行时间类型-在现有代码中,这是唯一的
引擎。ExecuteCommand
,因此不会调用程序中的代码。您正在创建引擎的实例,不是程序
-只需将Main的第一行
更改为:
Engine engine = new Program();
要使用的实现基于调用方法的对象的执行时间类型-在现有代码中,这是唯一的引擎.ExecuteCommand
,因此不会调用程序中的代码