代码:
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("输入1 或 2 ");
int name =Convert.ToInt32( Console.ReadLine());
GongC gongC = Fon.Fong(name);
if (gongC != null) {
Console.WriteLine("这是只");
gongC.gon();
}
Console.ReadLine();
}
}
//抽象类
public abstract class GongC
{
//抽象方法
public abstract void gon();
}
public class dog : GongC
{
public override void gon()
{
Console.WriteLine("狗狗");
}
}
public class Cat : GongC
{
public override void gon()
{
Console.WriteLine("喵喵");
}
}
//工厂
public class Fon
{
public static GongC Fong(int name)
{
switch (name)
{
case 1:
return new dog();
case 2:
return new Cat();
default:
return null;
}
}
}