using System;public interface ISomeInterface
{
    void SomeMethod();
}public interface IAnotherInterface : ISomeInterface
{
    void AnotherMethod();
}public class SomeClass : IAnotherInterface
{
    public void SomeMethod()
    {
        Console.WriteLine("SomeClass.SomeMethod()");    }
    public virtual void AnotherMethod()
    {
        Console.WriteLine("SomeClass.AnotherMethod()");
    }
}public class SomeDerivedClass : SomeClass
{
    public new void SomeMethod()
    {
        Console.WriteLine("SomeDerivedClass.SomeMehod()");
    }
    public override void AnotherMethod()
    {
        Console.WriteLine("SomeDerivedClass.AnotherMethod()");
    }
}public class EntryPoint
{
    static void Main()
    {
        SomeDerivedClass obj = new SomeDerivedClass();
        ISomeInterface isi = obj;
        IAnotherInterface iai = obj;        isi.SomeMethod();
        iai.SomeMethod();
        iai.AnotherMethod();
    }
}
结果是:
    SomeClass.SomeMethod()
   SomeClass.SomeMethod()
   SomeDerivedClass.AnotherMethod()求解释interface继承多态

解决方案 »

  1.   

    1.isi.SomeMethod();
    通过接口调用方法,执行实现接口方法的类中的方法,就是SomeClass中的SomeMethod(),因为类SomeDerivedClass中的SomeMethod方法被加了new,已经不是接口中的方法了,而屏蔽了接口中的方法
    2.iai.SomeMethod();
    同上
    3.iai.AnotherMethod();
    方法AnotherMetho使用override重写了基类中的方法,通过接口调用的时候会找到最终实现接口方法的方法执行,所以执行的是SomeDerivedClass类中的AnotherMethod方法