public abstract class Log
{
public abstract void Write();
}
public class EventLog : Log
{
public override void Write()
{
Console.WriteLine("EventLog Write Success!");
}
}
public class FileLog : Log
{
public override void Write()
{
Console.WriteLine("FileLog Write Success!");
}
}
public class LogFactory<T> where T:Log , new() // T 是约束为任何实现log类型的,且有无参数的构造方法的类型
{
public Log Create(T ty)
{
Log x= new T();
return x;
}
}
public class App
{
public static void Main(string[] args)
{
LogFactory<FileLog> factory = new LogFactory<FileLog>();
Log log = factory.Create();
log.Write();
Console.ReadLine();
}
}