代码改变世界

C#学习笔记(七):接口的执行

2006-01-13 10:38  努力学习的小熊  阅读(1154)  评论(0编辑  收藏  举报

一、接口的基本概念

接口的定义方式与类比较相似。

    interface IMyInterface

    {

        //interface members

    }

接口成员的定义与类成员的定义之间的区别:

1.不允许使用访问修饰符(publicprivateprotectedinternal),所有的接口成员都是公共的

2接口成员不能包含代码体

3接口不能定义域成员(属性)

4.接口成员不能使用关键字staticvirtualabstractsealed来定义。

5.类型定义成员是禁止的。

 

如果需要隐藏继承了基接口的成员,可以使用new关键字来定义它们,例如:

    interface IMyInterface

    {

        void DoSomething();

    }

 

    interface IMyDerivedInterface : IMyInterface

    {

        new void DoSomething();

    }

其执行方式与隐藏继承的类成员一样。

在接口中定义的属性可以确定访问块get/set中的哪一个能用于该属性。

    interface IMyInterface

    {

        int MyInt

        {

            get;

            set;

        }

    }

注意:接口没有指定属性应如何存储。接口不能指定域,例如用于存储属性数据的域。

接口与类一样,可以定义为类的成员(但与接口的其他成员不同,因为接口不能包含类型定义)。

 

二、在类中执行接口

执行接口的类必须包含该接口所有成员的执行代码,且必须匹配指定的签名(包括匹配指定的getset块),并且必须是公共的。

可以使用关键字virtualabstract来执行接口成员,但不能使用staticconst。例如:

    public interface IMyInterface

    {

        void DoSomething();

        void DoSomethingElse();

    }

 

    public class MyClass : IMyInterface

    {

        void IMyInterface.DoSomething()

        {

        }

 

        public void DoSomethingElse()

        {

        }

    }

接口成员还可以在基类上执行:

    public interface IMyInterface

    {

        void DoSomething();

        void DoSomethingElse();

    }

 

    public class MyBaseClass

    {

        public void DoSomething()

        {

        }

    }

 

    public class MyDerivedClass : MyBaseClass,IMyInterface

    {

        public void DoSomethingElse()

        {

        }

    }

即通过另一类中定义相同签名并且符合实现接口的方法。

 

继承一个执行给定接口的基类,就意味着派生类隐式地支持这个接口,例如:

    public interface IMyInterface

    {

        void DoSomething();

        void DoSomethingElse();

    }

 

    public class MyBaseClass : IMyInterface

    {

        public virtual void DoSomething()

        {

            Console.WriteLine("MyBaseClass->IMyInterface->DoSomething()");

        }

 

        public virtual void DoSomethingElse()

        {

            Console.WriteLine("MyBaseClass->IMyInterface->DoSomethingElse()");

        }

    }

 

    public class MyDerivedClass : MyBaseClass

    {

        public override void DoSomethingElse()

        {

            Console.WriteLine("MyDerivedClass->MyBaseClass->IMyInterface->DoSomethingElse()");

        }

    }

在基类中把执行代码定义为虚拟,派生类就可以替换该执行代码,而不是隐藏它们。如果要使用new关键字隐藏一个基类成员,而不是重写它,则方法IMyInterface.DoSomething()就总是引用基类版本,即使派生类通过这个接口来访问,也是这样。

我们用下面这段代码测试上边的接口定义:

            MyBaseClass a = new MyBaseClass();

            a.DoSomething();

            a.DoSomethingElse();

            IMyInterface imi = a;

            imi.DoSomething();

            imi.DoSomethingElse();

           

            Console.WriteLine("\n");

 

            MyDerivedClass b = new MyDerivedClass();

            b.DoSomething();

            b.DoSomethingElse();

            imi = b;

            imi.DoSomething();

            imi.DoSomethingElse();

执行结果为:

 

三、显示执行接口成员

接口成员也可以由类显示的执行。如果这么做,该成员就只能通过接口来访问,不能通过类来访问。

    public interface IMyInterface

    {

        void DoSomething();

        void DoSomethingElse();

    }

 

    public class MyBaseClass : IMyInterface

    {

        void IMyInterface.DoSomething()

        {

            Console.WriteLine("MyBaseClass->IMyInterface->DoSomething()");

        }

 

        public void DoSomethingElse()

        {

            Console.WriteLine("MyBaseClass->IMyInterface->DoSomethingElse()");

        }

    }

这样定义好后我们用如下代码进行执行:

            MyBaseClass a = new MyBaseClass();

            a.DoSomethingElse();

            IMyInterface imi = a;

            imi.DoSomething();

            imi.DoSomethingElse();

大家可以看下面的截图,对于DoSomething()我们只能通过接口才能访问。

类对象访问不到:


    
    接口对象可以访问到: