接口的显示实现

来源:互联网 发布:网络机房巡检 编辑:程序博客网 时间:2024/04/30 01:03

今天看书.发现有些教材上对接口的用法和实际的c#的语法有大的出入。

 

如  对于接口的实现中教材上讲的实现中直接就是实现接口中的方法。但是这个是编译不过去的。

 

如:

interface Father

{

   void printFather();

}

interface ChildA

{

   void printChildA();

}

interface ChildB()

{

   void printChildB();

}

public class Test

{

     void Father.printFather()//这里需要完全的接口限定名来声明  而在有些教材上是这样来做的 void printFather()  这是不正确的。

{

   ...........

}

void ChildB.printChildB()

{

   .....

}

void ChildA.printChildA()

{

   .....

}

}

//调用

using System;

public Class invoke

{

   public static void Main(string [] args)

{

   Test t = new Test();

 

t.printFather();//这样调用是不正确的。必须这样来调用

Father f = (Father)t;

f.printFather();//这样才可以  必须上溯

}

}

 

 

自己动手试试才知道有这样大的出入。要不然就要迷惑了。

 

 

还有一个问题。就是知道接口在构组件中的作用很大。可是我没有构建过。也不知道接口该怎么用。那位仁兄能不能举个实际的列子呢

原创粉丝点击