只是我学习Generics.When我有一个像抽象方法模式:工厂方法模式使用泛型-C#
//Abstract Product
interface IPage
{
string pageType();
}
//Concerete Product 1
class ResumePage : IPage
{
public string pageType()
{
return "Resume Page";
}
}
//Concrete Product 2
class SummaryPage : IPage
{
public string pageType()
{
return "SummaryPage";
}
}
//Fcatory Creator
class FactoryCreator
{
public IPage CreateOnRequirement(int i)
{
if (i == 1) return new ResumePage();
else { return new SummaryPage(); }
}
}
//Client/Consumer
void Main()
{
FactoryCreator c = new FactoryCreator();
IPage p;
p = c.CreateOnRequirement(1);
Console.WriteLine("Page Type is {0}", p.pageType());
p = c.CreateOnRequirement(2);
Console.WriteLine("Page Type is {0}", p.pageType());
Console.ReadLine();
}
如何使用泛型的代码转换?
什么是您的最终目标是什么?为什么你需要泛型? – 2010-03-23 12:54:35
也许他想学习如何在工厂模式中使用泛型? – Niike2 2010-03-23 12:58:21
@妮可,是的,你是对的。 – nanda 2010-03-23 13:03:40