c#继承中的函数调用
首先看下面的代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
using System;namespace Test{ public class Base { public void Print() { Console.WriteLine(Operate(8, 4)); } protected virtual int Operate(int x, int y) { return x + y; } }} |
|
1
2
3
4
5
6
7
8
9
10
|
namespace Test{ public class OnceChild : Base { protected override int Operate(int x, int y) { return x - y; } }} |
|
1
2
3
4
5
6
7
8
9
10
|
namespace Test{ public class TwiceChild : OnceChild { protected override int Operate(int x, int y) { return x * y; } }} |
|
1
2
3
4
5
6
|
namespace Test{ public class ThirdChild : TwiceChild { }} |
|
1
2
3
4
5
6
7
8
9
10
|
namespace Test{ public class ForthChild : ThirdChild { protected new int Operate(int x, int y) { return x / y; } }} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
namespace Test{ class Program { static void Main(string[] args) { Base b = null; b = new Base(); b.Print(); b = new OnceChild(); b.Print(); b = new TwiceChild(); b.Print(); b = new ThirdChild(); b.Print(); b = new ForthChild(); b.Print(); } }} |
看结果:

从结果中可以看出:使用override重写之后,调用的函数是派生的最远的那个函数,使用new重写则是调用new之前的派生的最远的函数,即把new看做没有重写似的。
c#继承中的函数调用的更多相关文章
- c#继承中的函数调用实例
using System; namespace Test { public class Base { public void Print() { ...
- C++ 类的继承四(类继承中的重名成员)
//类继承中的重名成员 #include<iostream> using namespace std; /* 自己猜想: 对于子类中的与父类重名的成员,c++编译器会单独为子类的这个成员变 ...
- C++ 类的继承三(继承中的构造与析构)
//继承中的构造与析构 #include<iostream> using namespace std; /* 继承中的构造析构调用原则 1.子类对象在创建时会首先调用父类的构造函数 2.父 ...
- C++继承中析构函数 构造函数的调用顺序以及虚析构函数
首先说说构造函数.大家都知道构造函数里就能够调用成员变量,而继承中子类是把基类的成员变成自己的成员,那么也就是说子类在构造函数里就能够调用基类的成员了,这就说明创建子类的时候必须先调用基类的构造函数, ...
- C++学习笔记-继承中的构造与析构
C++存在构造函数与析构函数,继承中也存在构造和析构函数.继承中的构造和析构函数与普通的构造析构有细微差别. 赋值兼容性原则 #include "iostream" using n ...
- 【转载】 C++多继承中重写不同基类中相同原型的虚函数
本篇随笔为转载,原文地址:C++多继承中重写不同基类中相同原型的虚函数. 在C++多继承体系当中,在派生类中可以重写不同基类中的虚函数.下面就是一个例子: class CBaseA { public: ...
- python 继承中的super
python继承中子类访问父类的方法(包括__init__)主要有两种方法,一种是调用父类的未绑定方法,另一种是使用super(仅仅对于新式类),看下面的两个例子: #coding:utf-8 cla ...
- 一个Public的字段引起的,谈谈继承中的new
一直觉得对c#面向对象这块已经掌握的很好了,因为正常情况下字段一般我们设计成私有的,今天突然想到一个实验,如下有两个很简单的类: public class Farther { ; public vir ...
- C++中构造函数调用构造函数
今天想做道矩阵的题目时,却卡在一些编程细节上了,找了好久才发现原来是在构造函数处出了问题,然后上网百度了下,发现这篇文章说得很好:从一道题谈C++中构造函数调用构造函数,很棒! 还补充一点: 看来自己 ...
随机推荐
- java新手笔记26 Frame
0.Calculater package com.yfs.javase; import java.awt.BorderLayout; import java.awt.Button; import ja ...
- Linux 驱动程序/内核模块/ko文件
Linux 驱动程序/内核模块/ko文件 一.内核模块加载机制 1.解析 Linux 内核可装载模块的版本检查机制 二.驱动/内核模块 编译 1.The Linux Kernel Module Pro ...
- WTL 中CComboBoxEx显示不了的问题
在使用WTL的CComboBoxEx时,InsertItem之后,运行程序,ComboBox显不了问题,其原因如下: I guess you want to place combo box to di ...
- CruiseControl.NET : Configuration Preprocessor
Original link: http://build.sharpdevelop.net/ccnet/doc/CCNET/Configuration%20Preprocessor.html http: ...
- Java标准输入输出流的重定向及恢复
在Java中输入输出数据一般(图形化界面例外)要用到标准输入输出流System.in和System.out,System.in,System.out默认指向控制台,但有时程序从文件中输入数据并将结果输 ...
- OpenJudge 2680 化验诊断 C++
链接地址:http://bailian.openjudge.cn/practice/2680 题目: 总时间限制: 1000ms 内存限制: 65536kB 描述 下表是进行血常规检验的正常值参考范围 ...
- 在Mac OS X中使用VIM开发STM32(2)
本文原创于http://www.cnblogs.com/humaoxiao,非法转载者请自重! 在我先前的博文⎣在Mac OS X中使用VIM开发STM32(1)⎤中,我们安装完成了MACVIM,这一 ...
- Node.js:JavaScript脚本运行环境
Node.js https://nodejs.org/ 2016-08-03
- textarea限定字数提示效果
最近工作中要实现的一个效果是:在textarea中输入字符会提示剩余多少字符可输入.于是马不停蹄的开始查阅资料. HTML代码: <table> <colgroup> < ...
- CheckedListBox与下拉框联动代码
private void yewubind(string id) { //给业务类型下拉框绑定业务类型数据 DataTable dtyewu = sb.SelectLast(id, 0); bool ...