using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication2
...{
class Program
...{
static void Main(string[] args)
...{
string FruitName = Console.ReadLine();
IFruit MyFruit;
FruitFactory MyFruitFactory = new FruitFactory();
MyFruit = MyFruitFactory.MakeFruit(FruitName);
Console.ReadLine();
}
}
public interface IFruit
...{
}
public class Orange : IFruit
...{
public Orange()
...{
Console.WriteLine("An Orange Fruit is got!");
}
}
public class Apple : IFruit
...{
public Apple()
...{
Console.WriteLine("A Apple Fruit is got!");
}
}
public class FruitFactory
...{
public IFruit MakeFruit(string name)
...{
switch (name)
...{
case "Orange":
return new Orange();
case "Apple":
return new Apple();
default:
return null;
}
}
}
/**//*
public class FruitFactory
{
public IFruit MakeFruit(string name)
{
IFruit MyFruit = null;
try
{
Type type = Type.GetType(name, true);
MyFruit = (IFruit)Activator.CreateInstance(type);
}
catch(TypeLoadException e)
{
Console.WriteLine("I don't know this kind of Fruit,exception caught-{0}",e.Message);
}
return MyFruit;
}
}*/
}
262

被折叠的 条评论
为什么被折叠?



