接口继承_1

2023-11-13

摘自Jeffrey的CLR via CSharp

接口方法默认是virtual and sealed. 意思是接口方法默认是没有继承的,这一点在你需要多态时需要注意。

Base b = new Base();  
Derived d = new Derived();  
b = new Derived();  
// Calls Dispose by using b's type: "Base's Dispose"  
b.Dispose();  
((IDisposable)b).Dispose();//通过把b转化为接口可以实现调用子类方法

 

由于没有多态,上述Dispose调用的基类的方法。

源码如下:

class Program
    {
        static void Main(string[] args)
        {
            /************************* First Example *************************/
            Base b = new Base();
            // Calls Dispose by using b's type: "Base's Dispose"
            //b.Dispose();
            // Calls Dispose by using b's object's type: "Base's Dispose"
            //((IDisposable)b).Dispose();
            /************************* Second Example ************************/
            Derived d = new Derived();
            // Calls Dispose by using d's type: "Derived's Dispose"
            //d.Dispose();
            // Calls Dispose by using d's object's type: "Derived's Dispose"

            //((IDisposable)d).Dispose();
            /************************* Third Example *************************/
            b = new Derived();
            // Calls Dispose by using b's type: "Base's Dispose"
            b.Dispose();
            // Calls Dispose by using b's object's type: "Derived's Dispose"
            ((IDisposable)b).Dispose();

        }
    }
    // This class is derived from Object and it implements IDisposable
    internal class Base : IDisposable
    {
        // This method is implicitly sealed and cannot be overridden
        public void Dispose()
        {
            Console.WriteLine("Base's Dispose");
        }
    }
    // This class is derived from Base and it re-implements IDisposable
    internal class Derived : Base, IDisposable
    {
        // This method cannot override Base's Dispose. 'new' is used to indicate
        // that this method re-implements IDisposable's Dispose method
        new public void Dispose()
        {
            Console.WriteLine("Derived's Dispose");
            // NOTE: The next line shows how to call a base class's implementation (if desired)
            // base.Dispose();
        }
    }

如果接口方法需要多态,源码可以改成如下:

internal class Base : IDisposable
    {
        // This method is implicitly sealed and cannot be overridden
        public virtual void Dispose()
        {
            Console.WriteLine("Base's Dispose");
        }
    }
    // This class is derived from Base and it re-implements IDisposable
    internal class Derived : Base, IDisposable
    {
        // This method cannot override Base's Dispose. 'new' is used to indicate
        // that this method re-implements IDisposable's Dispose method
        public override void Dispose()
        {
            Console.WriteLine("Derived's Dispose");
            // NOTE: The next line shows how to call a base class's implementation (if desired)
            // base.Dispose();
        }
    }

真正需要思考的问题是为什么微软要把接口方法默认为不可以继承的?


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

接口继承_1 的相关文章

  • Linux使用Note

    这个文档正在维护完善中 1 释放Swap空间 依次执行如下命令即可 xff0c span class token function sync span span class token builtin class name echo spa
  • Linux_note 命令grep,sed,awk

    1 grep 过滤出指定的行 grep cinvABC 39 word 39 filename color 把匹配到的关键词用红色标识 如 xff1a grep color 39 root 39 etc passwd c xff1a 打印符
  • linux note

    目录 快捷键 符号含义 系统目录颜色 系统根目录含义 ls al cd set env export source exec 快捷键 打开终端的快捷键的为 Ctrl 43 Alt 43 T 关闭终端的快捷键为 Ctrl 43 D 符号含义
  • A Survey on Concept Drift Adaptation Note

    A Survey on Concept Drift Adaptation Abstract Concept drift primarily refers to an online supervised learning scenario w
  • ML-Leaks Note

    ML Leaks Model and Data IndependentMembership Inference Attacks and Defenses onMachine Learning Models xff08 机器学习模型上与模型和
  • note: please ensure that VS 2013,VS 2015, VS 2017,VS 2019 or VS 2022 was instal1ed with the Visua1 C

    编译rust代码报错 报错信息 error 1inker link exe 96 not found 61 note program not found note the msvc targets depend on the msvc li
  • SAP有用的NOTE(持续更新)

    目录 2421240 Portal is not loaded on Chrome 56 or higher 66971 Supported SAP GUI platforms 66971 Supported SAP GUI platfor
  • mantis整合svn续:把提交的所有信息自动保存为note

    在前面两篇转载的文章中 xff0c 介绍了mantis和svn的整合 http blog csdn net newjueqi article details 7785373 http blog csdn net newjueqi artic
  • (Note)深度学习与人工提取的特征

    首先 xff0c 深度学习一般 不需要人工提取特征 如果仅仅给网络提供人工提取的特征 xff0c 反而有可能会造成网络性能的下降 xff08 深度学习模型可能提取到一些人类不易察觉的特征 xff0c 这些特征可能对结果的判定有着较大的贡献
  • linux-docker

    unix liunx windows linux 文件系统 所有的资源都是目录在 root 根目录下 一 指令 ip addr ifconfig cd ls vim sudo 管理员身份 代表换行输入 pwd 查看所在目录 sudo sys
  • Reference vs Pointer

    参考自Dan Saks的文章 An Introduction to References References and const The key insights I believe the key insight into why C
  • python 单例

    1 使用模块 from AA import a 2 使用 new 在Python中 可以通过重写类的 new 方法来实现单例模式 单例模式是一种设计模式 它保证一个类只有一个实例 并提供一个全局访问点 class Singleton obj
  • 把Collection转化为XML

    IList
  • AI三大主义:符号主义、联结主义、行为主义

    一 符号主义 symbolicism 符号主义 symbolicism 逻辑主义 Logicism 心理学派 Psychlogism 计算机学派 Computerism 其原理主要为物理符号系统 即符号操作系统 假设和有限合理性原理 早期的
  • 「转」plt.legend()简明使用教程

    原文链接https blog csdn net helunqu2017 article details 78641290 感谢作者辛勤付出 仅作笔记使用 侵删 1 图例legend基础语法及用法 legend语法参数如下 matplotli
  • LINQ的技术演进

    以一个简单的例子来说明 var developersUsingCSharp from d in developers where d Language C select d Name 1 提供对IEnumerable
  • Tensorflow(1)进行多维矩阵的拆分与拼接

    最近在使用tensorflow进行网络训练的时候 需要提取出别人训练好的卷积核的部分层的数据 由于tensorflow中的tensor和python中的list不同 无法直接使用加法进行拼接 后来发现一个函数可以完成tensor的拼接 函数
  • 当前的软件潮流

    1 以ERP为代表 注重业务逻辑 模式 数据存储 比较经典的书籍有Fowler的 lt 企业应用架构模式 gt 等 2 SaaS 一些通用型的企业需求越来越倾向于使用data center提供的服务 如CRM HCM human capit
  • tf1.x和tf2.x查看TFRecord数据的方法

    Tensorflow 1 x和Tensorflow 2 x读取tfrecord方法略有不同 下面分别记录两段代码 Tensorflow 1 x for example in tf python io tf record iterator p
  • Association Class VS Full Class

    详细分析请见 http etutorials org Programming UML Chapter 6 Class Diagrams Advanced Concepts Association Class 1 关联类的必要性 关联类隔离了

随机推荐

  • Tensorflow学习笔记(一)

    这里写自定义目录标题 Tensorflow 基本分类 对服装图像进行分类 电影评论文本分类 Basic regression Predict fuel efficiency 过拟合和欠拟合 过拟合 Tensorflow Tensorflow
  • American Sign Language手语识别(机器学习第二个小练习)

    American Sign Language手语识别 1 需要的数据集 链接 https pan baidu com s 1bf98hHYZbKuY 8hdfe8NRQ 提取码 uvzj 2 实验代码如下 import pandas as
  • winform 如何获取某个子控件的子控件并操作它

    一 panel1 Controls button1 或 panel1 Controls 0 二 比如某窗体的子控件为UserControl 且UserControl上有n多个n种控件 那么可以再UserControl里写属性获取
  • 【综合类型第 34 篇】喜讯!喜讯!!喜讯!!!,我在 CSDN 的第一个实体铭牌

    这是 综合类型第 34 篇 如果觉得有用的话 欢迎关注专栏 2022年7月19日 14 25 CSDN 官方博客给我发了一条私信 内容如下 为了表彰在 CSDN 社区发展中 做出 突出贡献 取得 优秀成绩 的用户 我们特地准备了实体铭牌 恭
  • EA12版本怎么恢复Project Browser

    其实是个小问题 不知道怎么就关闭Project Browser 了 找了好久才找到
  • springboot2.0从apollo读取配置

    参考 https github com nobodyiam apollo build scripts 本篇文章包括两点 Windows环境下apollo单机搭建 springboot2 0从apollo读取配置 一 windows环境下ao
  • kubectl查看容器日志

    kubectl get pod n 命名空间 kubectl logs 容器名称 n 命名空间
  • 青龙脚本 小黄鸟配合虚拟机抓变量 软件集

    鉴于很多青龙脚本变量 真机很难抓到 需要小黄鸟配合虚拟机抓变量 为方便网友 把相关3个软件分享 以单个文件形式 1 小黄鸟 https wwm lanzouy com i5vWT01qi6na 2 虚拟机 https wwm lanzouy
  • nvcc使用指定gcc版本(不改变全局gcc版本)

    在nvcc后面加上 compiler bindir usr bin gcc x即可 x为指定的gcc版本号
  • MSP430F5529学习笔记(6)——导入MSP430Ware,查看例程

    MSP430WARE下载 目录 在线版本 下载MSP430Ware 查看例程 导入例程 离线版本 下载MSP430Ware 查看例程 导入例程 MSP430Ware里面有很多例程和库函数使用手册 我们可以查看学习 非常重要 在线版本 下载M
  • 使用计算机结束时断开终端的连接属于什么,计算机结束时断开终端的连接属于什么...

    计算机结束时断开终端的连接属于外部终端的物理安全 终端安全管理 endpoint security management 是一种保护网络安全的策略式方法 它需要终端设备在得到访问网络资源的许可之前遵从特定的标准 推荐学习 web前端视频教程
  • 【数模】插值算法

    插值算法的介绍 插值的作用 当现有的数据是极少的 不足以支撑分析的进行时 用于 模拟产生 一些新的但又比较靠谱的值来满足需求 插值函数 插值 插值法的概念 插值法的分类 插值多项式 P x 为次数不超过n的代数多项式 即 数模中也常见 但不
  • ARM——体系架构

    1 ARM简介 ARM是Advanced RISC Machines的缩写 它是一家微处理器行业的知名企业 该企业设计了大量高性能 廉价 耗能低的RISC 精简指令集 处理器 公司的特点是只设计芯片 而不生产 它将技术授权给世界上许多著名的
  • Centos下使用脚本快速安装GO语言环境

    Centos使用shell脚本快速安装go环境并安装spaceVim IDE 脚本如下 bin bash env git install echo 安装依赖中 sudo yum y install make autoconf automak
  • Tomcat配置context.xml问题

    关于Tomcat的配置文件问题 请参考Apache Tomcat官网Document菜单 根据版本号选择恰当的Reference 我使用的环境 netbeans 内嵌 Tomcat 8 0 1
  • 彻底删除VMware虚拟机

    您是否和我一样被VMware气到了呢 您是否再也不想理VMware了呢 您是否不想再在自己电脑上看到VMware这几个英文字母了呢 来吧 跟着我的步骤 一起和VMware说拜拜吧 一 在卸载VMware虚拟机之前 要先把与VMware相关的
  • 工业上的数控机床所属计算机应用的什么领域,以下哪一项不是企业初步战略方案包括的内容?()。...

    摘要 弹簧振子的振幅增加一倍 下业初则 步战工业机器人最重要的核心能力 社会学是指一门科学 略方即它以解释的方式来理解社会行动 略方据此 通过社会行为的过程及其结果 对社会行为作出因果解释 因此 社会学的研究对象是 研究的方法是 弹簧振子的
  • 西门子S7 模拟器使用教程

    一 S7协议概述 S7协议是西门子S7系列PLC通信的核心协议 它是一种位于传输层之上的通信协议 其物理层 数据链路层可以是MPI总线 PROFIBUS总线或者工业以太网 S7以太网协议本身也是TCP IP协议簇的一员 S7协议在OSI中的
  • PCB中过孔和通孔焊盘的区别

    在PCB设计中 过孔VIA和焊盘PAD都可以实现相似的功能 它们都能插入元件管脚 特别是对于直插 DIP 封装的的器件来说 几乎是一样的 但是 在PCB制造中 它们的处理方法是不一样的 1 VIA的孔在设计中表明多少 钻孔就是多少 然后还要
  • 接口继承_1

    摘自Jeffrey的CLR via CSharp 接口方法默认是virtual and sealed 意思是接口方法默认是没有继承的 这一点在你需要多态时需要注意 Base b new Base Derived d new Derived