日期:2014-05-17 浏览次数:21100 次
public class D
{
public A a { get; set; }
public B b { get; set; }
public C c { get; set; }
}
public class E : D {}
......
static void Main()
{
Console.WriteLine(DoSomething(new E()));
Console.WriteLine(DoSomething(new D()));
}
static string DoSomething(D obj)
{
......
}
public interface IDoSomething
{
public string DoSomething();
}
public class E : IDoSomething
{
...... <- 把实现接口的时候需要用到的东西放过来
public string DoSomething()
{
...... <- 具体干活,参考以前的DoSomething(D obj)
}
}
public class D : IDoSomething
{
...... <- 和以前一样
public string DoSomething()
{
...... <- 具体干活,参考以前的DoSomething(D obj)
}
}
static void Main()
{
Console.WriteLine(DoSomething(new E()));
Console.WriteLine(DoSomething(new D()));
}
static string DoSomething(IDoSomething obj) <- 传入的是接口,所以D和E都能用
{
...... <- 不用管obj具体是什么,只要拿到obj.DoSomething()就可以了
}