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

news/2024/7/6 13:38:20

一、接口的基本概念<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

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

    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();

执行结果为:

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />

C%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%83%EF%BC%89%EF%BC%9A%E6%8E%A5%E5%8F%A3%E7%9A%84%E6%89%A7%E8%A1%8C1.JPG 

三、显示执行接口成员

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

    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()我们只能通过接口才能访问。

类对象访问不到:

C%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%83%EF%BC%89%EF%BC%9A%E6%8E%A5%E5%8F%A3%E7%9A%84%E6%89%A7%E8%A1%8C2.JPG
    
    接口对象可以访问到:

C%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%EF%BC%88%E4%B8%83%EF%BC%89%EF%BC%9A%E6%8E%A5%E5%8F%A3%E7%9A%84%E6%89%A7%E8%A1%8C3.JPG

转载于:https://www.cnblogs.com/Bear-Study-Hard/archive/2006/01/13/316564.html


http://www.niftyadmin.cn/n/1197165.html

相关文章

宝塔面板 使用教程

如果你要安装宝塔linux面板,你要准备好一个纯净版的linux操作系统&#xff0c;没有安装过其它环境带的Apache/Nginx/php/MySQL&#xff08;已有环境不可安装&#xff09;。支持的操作系统有CentOS&#xff0c;Ubuntu、Debian、Fedora。这里给大家演示的是centos7.5。 1,通过ssh…

centOS和宝塔linux面板详细使用

本文章可是用于个人发布项目,也可以用于团队上线项目参考. 前提:买一个服务器,一个域名 (个人使用和平时练习可以不买域名) linux系统如下操作&#xff1a; 国内服务器:如阿里云 腾讯云 1. 先安装centOS系统,因为系统原因,没有可视化页面,所以要安装宝塔linux,(宝塔linux是…

Aaron Stannard谈Akka.NET 1.1

Akka.NET 1.1近日发布&#xff0c;带来新特性和性能提升。InfoQ采访了Akka.net维护者Aaron Stannard&#xff0c;了解更多有关Akka.Streams和Akka.Cluster的信息。Aaron还阐述了与Akka for JVM实现有关的路线图计划。\\InfoQ&#xff1a;这个版本有什么突出的特性&#xff1f;\…

一些网页效果

1、忽视右键 <body οncοntextmenu"return false">或<body style"overflow-y:hidden"> 2、加入背景音乐 IE:<bgsound src"*.mid" loopinfinite> NS:<embed src"*.mid" autostarttrue hiddentrue looptrue>&…

Windows连接阿里云服务器图形界面

文章目录 前言Motivation of writing1 场景说明2 给阿里云服务器安装图形界面3 给服务器配置VNC Server4 让服务器放行5901端口5 本地windows配置vnc viewer总结标题&#xff1a;本地Windows远程连接阿里云服务器图形界面 前言 时隔数月&#xff0c;又要开始认真用服务器了。…

Poj(1703),种类并查集

题目链接&#xff1a;http://poj.org/problem?id1703 已经不是第一次接触种类并查集了&#xff0c;直到今天才搞懂。 感谢红黑联盟&#xff0c;感谢杰哥&#xff01;&#xff01;&#xff01; 每个节点只要关系确定&#xff0c;不管是不是同一个集合里面&#xff0c;都把他们放…

三千年来振奋过中国人的29句口号(是中国人就看看!)

一、古代中国&#xff08;从先秦到1840&#xff09;&#xff1a;   春秋战国时期&#xff1a;公元前770年&#xff0d;公元前221年   1&#xff1a;天行健&#xff0c;君子以自强不息。———《易经》   2&#xff1a;发奋忘食&#xff0c;乐以忘优&#xff0c;不知老之将…

阿里云-轻量应用服务器-Ubuntu-图形界面-xfce-VNC连接

1.在阿里云上申请一个 轻量应用服务器 阿里云&#xff1a;https://www.aliyun.com/ 配置选好,系统选Ubuntu&#xff0c;下单付款。然后就去管理控制台。 我当初是选了CentOS,但想着想着又把系统换回Ubuntu了&#xff0c;所以这里的名字是CentOS,但实际上里面是Ubuntu 进入到管…