您好 我想知道这个例子是否有最佳实践代码或更简单的提示。 所有类都是Choix的子类,但所有方法都在子类中重写。 如果我尝试使用Choix Choix = new AddTenant但没有成功 感谢您的任何进一步帮助/回答
string arguments = args[0].ToLower();
bool load;
bool lauch;
bool result;
switch (arguments)
{
case "AddTenant":
AddTenant AddTenant = new AddTenant();
load = AddTenant.Load();
lauch = AddTenant.Lauch();
result = AddTenant.Result();
break;
case "BackupDb":
BackupDb BackupDb = new BackupDb();
load = BackupDb.Load();
lauch = BackupDb.Lauch();
result = BackupDb.Result();
break;
case "ExtractTenant":
ExtractTenant ExtractTenant = new ExtractTenant();
load = ExtractTenant.Load();
lauch = ExtractTenant.Lauch();
result = ExtractTenant.Result();
break;
case "InstallDb":
InstallDb InstallDb = new InstallDb();
load = InstallDb.Load();
lauch = InstallDb.Lauch();
result = InstallDb.Result();;
break;
case "RestoreDb":
RestoreDb RestoreDb = new RestoreDb();
load = RestoreDb.Load();
lauch = RestoreDb.Lauch();
result = RestoreDb.Result();
break;
case "UpgradeTenant":
UpgradeTenant UpgradeTenant = new UpgradeTenant();
load = UpgradeTenant.Load();
lauch = UpgradeTenant.Lauch();
result = UpgradeTenant.Result();
break;
default:
Help Help = new Help();
load = Help.Load();
lauch = Help.Lauch();
result = Help.Result();
break;
}
Console.WriteLine(load+" "+lauch+" "+result);
答案 0 :(得分:0)
如果这些类是Choix的子类,并且方法被正确覆盖,那么您的建议应该起作用:
string arguments = args[0].ToLower();
Choix choix;
switch (arguments)
{
case "AddTenant":
choix = new AddTenant();
break;
case "BackupDb":
choix = new BackupDb();
break;
case "ExtractTenant":
choix = new ExtractTenant();
break;
case "InstallDb":
choix = new InstallDb();
break;
case "RestoreDb":
choix = new RestoreDb();
break;
case "UpgradeTenant":
choix = new UpgradeTenant();
break;
default:
choix = new Help();
break;
}
bool load = choix.Load();
bool lauch = choix.Lauch();
bool result = choix.Result();
Console.WriteLine(load + " " + lauch + " " + result);
这是您的课程应该如下所示的选项:
abstract class Choix
{
public abstract bool Load();
public abstract bool Lauch();
public abstract bool Result();
}
class AddTenant : Choix
{
public override bool Load()
{
// Load ...
return true;
}
public override bool Lauch()
{
// Lauch ...
return true;
}
public override bool Result()
{
// Result ...
return true;
}
}