111,126
社区成员
发帖
与我相关
我的任务
分享
static class SimpleFac
{
public enum TypeStr
{
car,
boat
}
public static Ab_vechile GetVehcle(Enum e)
{
Ab_vechile v;
//想根据传入的枚举值返回创建的对象,错误提示要求输入整型值
switch (e)
{
case TypeStr.car:
v = new car();
break;
case TypeStr.boat:
v = new boat();
break;
default:
//想只打印一个提示,不创建对象,运行时也会报错,NullReferenceException
Console.WriteLine("输入错误!");
break;
}
return new car();
}
}
class Program
{
static void Main(string[] args)
{
//传入枚举值
Ab_vechile v = SimpleFac.GetVehcle(SimpleFac.TypeStr.car);
v.Run();
}
}