c# 接口详解2

下面的程序代码将这样实现。

P13.cs

程序代码:
class Demo : abc, def {
        public static void Main() {
            System.Console.WriteLine("Hello Interfaces");
            Demo refDemo = new Demo();
            abc refabc = refDemo;
            refabc.xyz();
            def refdef = refDemo;
            refdef.pqr();
        }

        public void xyz() {
            System.Console.WriteLine("In xyz");
        }

        public void pqr() {
            System.Console.WriteLine("In pqr");
        }
    }

    interface abc {
        void xyz();
    }

    interface def {
        void pqr();
    }


输出:

Hello Interfaces
In xyz
In pqr



上面的代码编译运行程序产生了预期的输出结果。类Demo实现了接口abc和def。一个类Demo的对象创建并将引用存储在refDemo中。refabc是类型为接口abc的变量指向类Demo的实例。在Deom中的xyz()方法可以通过refabc来方法因为refabc是接口abc的变量接口abc包含了方法xyz()的原型。同样,refdef是类型为def的变量指向类Demo的实力。可以通过refdef来方法Demo中的pqr()方法,因为refdef是类型为接口def的变量,接口def中包括了方法pqr()的原型。

 

P14.cs

程序代码:
class Demo : abc, def {
        public static void Main() {
            System.Console.WriteLine("Hello Interfaces");
            Demo refDemo = new Demo();
            abc refabc = refDemo;
            refabc.xyz();
            def refdef = refDemo;
            refdef.xyz();
        }

        public void xyz() {
            System.Console.WriteLine("In xyz");
        }
    }

    interface abc {
        void xyz();
    }

    interface def {
        void xyz();
    }


输出:

Hello Interfaces
In xyz
In xyz



上面的代码编译运行程序产生了预期的输出结果。接口abc和def都定义了方法xyz()的原型。类Demo实现接口abc和def并也实现了xyz()的方法。因此可以通过接口变量(refabc或refdef)存储在refabc或refdef的Demo的对象引用来访问方法xyz()。假设一个问题,实现接口abc中的xyz()的方法和实现接口def中的xyz()的方法是不同的在类Demo中如何实现?此时需要按下面的程序使用完全限定名来实现。

 

P15.cs

程序代码:
class Demo : abc, def {
        public static void Main() {
            System.Console.WriteLine("Hello Interfaces");
            Demo refDemo = new Demo();
            abc refabc = refDemo;
            refabc.xyz();
            def refdef = refDemo;
            refdef.xyz();
        }

        public void abc.xyz() {
            System.Console.WriteLine("In abc.xyz");
        }

        public void def.xyz() {
            System.Console.WriteLine("In def.xyz");
        }

    }

    interface abc {
        void xyz();
    }

    interface def {
        void xyz();
    }


输出:

a.cs(13,15): error CS0106: The modifier 'public' is not valid for this item
a.cs(18,15): error CS0106: The modifier 'public' is not valid for this item



失败!使用了全缀名得到错误。原因是当为接口中的部分方法原型使用全缀名是,编译器不能使用想public的修饰符。因此从上面的代码中去掉public。

 

P16.cs

程序代码:
class Demo : abc, def {
        public static void Main() {
            System.Console.WriteLine("Hello Interfaces");
            Demo refDemo = new Demo();
            abc refabc = refDemo;
            refabc.xyz();
            def refdef = refDemo;
            refdef.xyz();
        }

        void abc.xyz() {
            System.Console.WriteLine("In abc.xyz");
        }

        void def.xyz() {
            System.Console.WriteLine("In def.xyz");
        }

    }

    interface abc {
        void xyz();
    }

    interface def {
        void xyz();
    }


输出:

Hello Interfaces
In abc.xyz
In def.xyz



上面的代码编译运行程序产生了预期的输出结果。系统允许定义接口有相同的方法原型使使用全缀名。在上面的例子中,接口abc和def包含相同的方法原型xyz()。类Demo实现了上面两个接口。在实现方法xyz()可以使用全缀名来制定实现接口abc中的方法xyz()方法还是实现接口def中的xyz()方法。

 

P17.cs
程序代码:
    class Demo : def {
        public static void Main() {
            System.Console.WriteLine("Hello Interfaces");
            Demo refDemo = new Demo();
            def refdef = refDemo;
            refdef.xyz();
            refdef.pqr();
        }

        public void xyz() {
            System.Console.WriteLine("In xyz");
        }

        public void pqr() {
            System.Console.WriteLine("In pqr");
        }

    }

    interface abc {
        void xyz();
    }

    interface def : abc {
        void pqr();
    }

输出:
Hello Interfaces
In xyz
In pqr


上面的代码编译运行程序产生了预期的输出结果。接口支持继承。接口def从接口abc中继承了方法原型。类demo实先def接口。接口变量refdef存储了类Demo的对象的引用。可以通过接口变量refdef的引用来访问类Demo中的xyz()和pqr()方法。

接口def继承自接口abc思考如何使用xyz()和pqr()的全缀名。

P18.cs

程序代码:
class Demo : def {
        public static void Main() {
            System.Console.WriteLine("Hello Interfaces");
            Demo refDemo = new Demo();
            def refdef = refDemo;
            refdef.xyz();
            refdef.pqr();
        }

        void def.xyz() {
            System.Console.WriteLine("In xyz");
        }

        void def.pqr() {
            System.Console.WriteLine("In pqr");
        }

    }

    interface abc {
        void xyz();
    }

    interface def : abc {
        void pqr();
    }


输出:

P17.cs(12,8): error CS0539: 'def.xyz' in explicit interface declaration is not a member of interface
P17.cs(29,11): (Location of symbol related to previous error)
P17.cs(1,7): error CS0535: 'Demo' does not implement interface member 'abc.xyz()'
P17.cs(26,8): (Location of symbol related to previous error)



失败!xyz方法原型是接口abc的原始成员。因此,即使接口def基础自接口abc,但方法xyz()的全缀名需要是abc.xyz而不是上面程序中的def.xyz。事实上,会获得编译错误。在下面代码中使用正确的全缀名来修复错误。

 

P19.cs

程序代码:
class Demo : def {
        public static void Main() {
            System.Console.WriteLine("Hello Interfaces");
            Demo refDemo = new Demo();
            def refdef = refDemo;
            refdef.xyz();
            refdef.pqr();
        }

        void abc.xyz() {
            System.Console.WriteLine("In xyz");
        }

        void def.pqr() {
            System.Console.WriteLine("In pqr");
        }

    }

    interface abc {
        void xyz();
    }

    interface def : abc {
        void pqr();
    }


输出:

Hello Interfaces
In xyz
In pqr



上面的代码编译运行程序产生了预期的输出结果。但在使用全缀名时有一些小的陷阱。

 

P20.cs

程序代码:
class Demo : def {
        public static void Main() {
            System.Console.WriteLine("Hello Interfaces");
            Demo refDemo = new Demo();
            refDemo.xyz();
            refDemo.pqr();
        }

        void abc.xyz() {
            System.Console.WriteLine("In xyz");
        }

        void def.pqr() {
            System.Console.WriteLine("In pqr");
        }

    }

    interface abc {
        void xyz();
    }

    interface def : abc {
        void pqr();
    }


输出:

P19.cs(7,5): error CS0117: 'Demo' does not contain a definition for 'xyz'
P19.cs(8,5): error CS0117: 'Demo' does not contain a definition for 'pqr'



上面代码是失败的。refDemo是类Demo的对象。可以通过refDemo调用xyz()方法。因为C#xyz()和ddd.xyz()是不相同的事情。abc.xyz()是存在的。xyz()不在类中。将得到错误信息说类Demo没有包含方法xyz()的定义。因为C#中abc.xyz()仅仅包含在类型abc接口的引用。相同的,def.pqr()仅能够通过类型为def的应用来调用。

posted @ 2011-08-08 16:32  甜菜波波  阅读(471)  评论(0)    收藏  举报