我有一段如下代码,定义一个接口iInterface,cBase实现iInterface,cChild继承cBase,UML为

预期是想要cBase.F()的执行逻辑,同时需要cChild的返回值,所以FF预期的输出

   1:  namespace ConsoleApplication1
   2:  {
   3:      class Program
   4:      {
   5:          static void Main(string[] args)
   6:          {
   7:              iInterface inf = new cChild();
   8:              FF(inf);
   9:   
  10:              Console.ReadLine();
  11:          }
  12:          static public void FF(iInterface inf)
  13:          {
  14:              Console.WriteLine(inf.F());
  15:          }
  16:      }
  17:   
  18:      public interface iInterface
  19:      {
  20:          string F();
  21:      }
  22:      public class cBase : iInterface
  23:      {
  24:          public  string F()
  25:          {
  26:              Console.WriteLine("base.F1");
  27:              return "this is base";
  28:          }
  29:      }
  30:      public class cChild : cBase
  31:      {
  32:          public string F()
  33:          {
  34:              base.F();
  35:              return "this is child";
  36:          }
  37:      }
  38:  }

.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }

但是实际的结果是只能call到cBase的F函数,输出”this is base

原因是我们定义的inf类型为iInterface,同时赋值的对象类型为cChild,但是cChild是没有实现iInterface的(继承至实现了接口的class也是不行的)。

实际运行时,inf向上找到了cBase有实现iInterface,所以调用了它的实现。

想达到预期的结果,可以将cChild也实现iInterface,如下:

   1:  public class cChild : cBase, iInterface
   2:      {
   3:          public string F()
   4:          {
   5:              base.F();
   6:              return "this is child";
   7:          }
   8:      }

修改后的UML为

输出结果为:

Interface的多层继承的更多相关文章

  1. Python 在子类中调用父类方法详解(单继承、多层继承、多重继承)

    Python 在子类中调用父类方法详解(单继承.多层继承.多重继承)   by:授客 QQ:1033553122   测试环境: win7 64位 Python版本:Python 3.3.5 代码实践 ...

  2. java代码继承------多层继承

    总结:继承.方法的重要性, 运行结果显示: class A is callingclass B is callingclass C is calling package com.addd; //jav ...

  3. 零基础逆向工程24_C++_01_类_this指针_继承本质_多层继承

    1 类内的成员函数和普通函数的对比 1.1 主要是从参数传递.压栈顺序.堆栈平衡来总结. 1.参数传递:成员函数多传一个this指针 2.压栈顺序:成员函数会将this指针压栈,在函数调用取出 3.堆 ...

  4. C++逆向分析----虚函数与多层继承

    虚函数 C++通过关键字virtual来将函数声明为一个虚函数.当一个类包含虚函数后编译器就会给类的实例对象增加一个虚表指针,所谓的虚表指针就是指向虚表的指针.虚表就是一张地址表,它包含了这个类中所有 ...

  5. 『无为则无心』Python面向对象 — 55、多层继承和继承中的私有成员

    目录 1.Python支持多层继承 (1)多层继承实现 (2)多层继承和多重继承区别 2.继承中的私有成员 (1)继承中父类私有属性和私有方法 (2)获取和修改私有属性值 1.Python支持多层继承 ...

  6. 接口interface,接口继承implements

    php中,只支持从一个类继承,不支持从两个或者更多的类同时继承.从两个或者两个以上的类继承的能力被称为多重继承.php在设计上是禁止这种功能的.原因在于,避免多个类带来的复杂性.当发现需要从两个或者更 ...

  7. error C2504 类的多层继承 头文件包含

    error C2504:头文件包含不全 今天碰到了很烦的问题,继承一个类之后,感觉头文件都包含了,可还是出现父类未定义的问题,最后发现,子类的子类在实现时,需要在cpp文件中包含所有他的父类的定义.因 ...

  8. Python中类的多层继承和多重继承

  9. C# interface 的特性 无法被implement class继承

    最近做interface添加特性后,implement class 无法继承. 微软要求class是实现Interface而不是继承,所以我们必须手动添加特性,而不能自动继承. 对于abstract ...

随机推荐

  1. Struts2 Action的访问路径

    1.     Action的访问路径 扩展名 缺省以.action结尾,请参考:default.properties文件,可以通过配置改变这一点: <constant name="st ...

  2. android开发问题汇总

    android开发问题汇总 一. 无法创建新android项目 在eclipse创建新项目时出现: this template depends on the android support libra ...

  3. 学习PHP时的一些总结(五)

    mysql中启用事务的数据表类型建议使用InnoDB 利用PHP代码调用mysql中的事务过程: 1>关闭自动提交过程 $mysqli->autocommit(0); 2>执行sql ...

  4. Upgrade Guide

    Upgrade Guide This guide will point out the key points to be aware of when upgrading to version 3. A ...

  5. PHP代码安全学习笔记V1.0

    PHP代码安全学习笔记V1.0http://www.docin.com/p-778369487.html

  6. logstash-input-file 参数说明

    参数 close_older close_older: # This has different implications depending on if a file is being tailed ...

  7. (原创)openvswitch实验连载2-cisco模拟器IOU-Web安装及网络环境配置

    IOU的使用可以选择自己到官网https://docs.google.com/file/d/0B2AgRhS2cfxCQ01tN2NrQ0pvNUk/edit下载iou-web 软件,然后部署在lin ...

  8. css扁平化博客学习总结(一)模块分析

    一.模块分析 1.每开发一个项目之前,首先要对项目进行一个大致规划,它到底要做什么功能,它有什么具体需求. 2.所以需要进行模块化分析,把这些东西具象化,把一个问题模块化,对需求有一个宏观的了解. 3 ...

  9. 3 WPF之从0开始学习XMAL

    转载:http://blog.csdn.net/fwj380891124/article/details/8088233   剖析最简单的XMAL代码: <Window x:Class=&quo ...

  10. spring3.2.0与mybatis3.2.7整合出错--Failed to read candidate component class--nested exception is java.lang.IllegalArgumentException

    错误信息如下: org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate com ...