事实上它还是使用Doc来对象化了啊

解决方案 »

  1.   

    how did you implement the interface? if you implement the interface this way:class TestInt : IEncryptable
    {
       public void Encrypt() {Console.WriteLine("Encrypt");}
       public void Decrypt() {Console.WriteLine("Decrypt");}
       //.... 
    }
    then you call them directly:TestInt doc  = new TestInt();
    doc.Encrypt();
    doc.Decrypt();if you implement the interface this way:class TestInt : IEncryptable
    {
       void IEncryptable.Encrypt() {Console.WriteLine("Encrypt");}
       void IEncryptable.Decrypt() {Console.WriteLine("Decrypt");}
       //....
    }then you have to do a cast before call its methods: TestInt doc  = new TestInt();
    IEncryptable ie = doc as IEncryptable;
    ie.Encrypt();
    ie.Decrypt();
    it is called Explicit Interface Implementation, see
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csref/html/vcwlkexplicitinterfaceimplementationtutorial.asp"...
    When a member is explicitly implemented, it cannot be accessed through a class instance, but only through an instance of the interface
    ..."
      

  2.   

    这就是接口与抽象类的区别了。说清楚这个问题不容易,得有大量编码实践,才能理解。我也理解的不透彻,不过简单说一点吧:使用接口不会影响类的继承层次,你可以在类的任意层次引入,而不破坏类的层次结构。比如你举的IEncryptable接口,它可以实现对各种流的加密,只要某个流类继承并实现该接口(这些流类不要求是一颗继承树),而不会影响到原来的类层次。假设用抽象类实现的话,那么在哪个类上实现好呢?又在类的哪个层次上实现好呢?