c# base 和this 继承
父类的构造函数总是在子类之前执行的。既先初始化静态构造函数,后初始化子类构造函数。


public class BaseCircle {
public BaseCircle()
{
Console.WriteLine(" no arguments base constructor!!!");
}
public BaseCircle(double arg)
{
Console.WriteLine("double arg base constructor!!!");
}
} public class SubCircle : BaseCircle {
public SubCircle():base()
{
Console.WriteLine("sub class no argument constructor,actually call base constructor !!!");
} public SubCircle(double a):base(a)
{
Console.WriteLine("sub class with argument, actually call base double constructor!!!");
} public SubCircle(int k):this(1,2)
{
Console.WriteLine("sub class with argument int k, actually call sub class constructor int i & j !!!");
} public SubCircle(int i,int j)
{
Console.WriteLine("sub class with int i&j argument!!!!");
}
} static void Main(string[] args)
{
SubCircle s1 = new SubCircle();
SubCircle s2 = new SubCircle(1.1);
SubCircle s3 = new SubCircle(1);
}




输出结果:
no arguments base constructor!!!
sub class no argument constructor,actually call base constructor !!! double arg base constructor!!!
sub class with argument, actually call base double constructor!!! no arguments base constructor!!!
sub class with int i&j argument!!!!
sub class with argument int k, actually call sub class constructor int i & j !!!


用法二:
是不是很模糊这两个关键字那?
哈,现在我来写份代码,代码可是最有说服力的啦!


class BaseClass
{
private int numA;
public BaseClass()
{
Console.WriteLine("基类的无参数构造函数. value:{0}", numA);
}
public BaseClass(int i)
{
this.numA = i;
Console.WriteLine("基类带一个参数的构造函数. value:{0}", numA);
}
}
class ChildClassA : BaseClass
{
private int numB;
public ChildClassA()
{
Console.WriteLine("子类无参数构造函数. value:{0}", numB);
}
public ChildClassA(int i)
{
this.numB = i;
Console.WriteLine("子类带有一个参数的构造函数. value:{0}", numB);
}
public ChildClassA(int i, int j)
: base(i)
{
this.numB = j;
Console.WriteLine("子类带有两个参数的构造函数. value:{0}", numB);
}
}
class ChildClassB : BaseClass
{
private int numB;
public ChildClassB()
{
Console.WriteLine("子类无参数构造函数. value:{0}", numB);
}
public ChildClassB(int i)
{
this.numB = i;
Console.WriteLine("子类带有一个参数的构造函数. value:{0}", numB);
}
public ChildClassB(int i, int j)
: this(i)
{
this.numB = j;
Console.WriteLine("子类带有两个参数的构造函数. value:{0}", numB);
}
}
class Demo
{
static void Main(string[] args)
{
Console.WriteLine("使用base\n");
ChildClassA a = new ChildClassA(2, 4);
Console.WriteLine();
Console.WriteLine("----------------------------------------\n");
Console.WriteLine("使用this\n");
ChildClassB b = new ChildClassB(2, 4);
Console.ReadKey();
}
}


执行的结果如下:


--------------------------------结果---------------------------------- 使用base 基类带一个参数的构造函数. value:2
子类带有两个参数的构造函数. value:4 ---------------------------------------- 使用this 基类的无参数构造函数. value:0
子类带有一个参数的构造函数. value:2
子类带有两个参数的构造函数. value:4 --------------------------------结果--------------------------------


this只是调用本身,但是这样是需要调用一次基类没有参的构造函数,所以会多显示一条“基类的无参数构造函数. value:0”。
base是调用基类的有参数构造函数。
c# base 和this 继承的更多相关文章
- c# base和this关键字总结
base:用于在派生类中实现对基类公有或者受保护成员的访问,但是只局限在构造函数.实例方法和实例属性访问器中.MSDN中小结的具体功能包括: (1)调用基类上已被其他方法重写的方法. ( ...
- Yii2的深入学习--继承关系
想要了解 Yii2 的话,一定要对 Yii2 中相关类的继承关系有所了解.由于暂时读的代码有限,下面的图中只列出了部分继承关系,之后回跟着源码阅读的越来越多而增加 由上图可以看到 Yii2 中大多数类 ...
- Python 类继承,__bases__, __mro__, super
Python是面向对象的编程语言,也支持类继承. >>> class Base: ... pass ... >>> class Derived(Base): ... ...
- C++中的类继承(1) 三种继承方式
继承是使代码可以复用的重要手段,也是面向对象程序设计的核心思想之一.简单的说,继承是指一个对象直接使用另一对象的属性和方法.继承呈现了 面向对象程序设 计的层次结构, 体现了 由简单到复杂的认知过程. ...
- Django(五)母版继承、Cookie、视图装饰器等
大纲 一.内容回顾 补充:默认值 补充:命名空间 二.模板语言 1.母版继承 2.include 3.自定义simple_tag 三.Cookie Cookie 使用总结 四.视图 1.获取用户请求相 ...
- base和this的用法
[意义] this:指当前类,this调用当前类的属性,方法,包括构造函数的方法,继承本类的构造函数 base:指当前类的父类,可调用父类的非私有属性,方法,继承父类的构造函数括号里的参数 [用处] ...
- Python3 与 C# 面向对象之~继承与多态
2.继承¶ 代码裤子:https://github.com/lotapp/BaseCode 在线编程:https://mybinder.org/v2/gh/lotapp/BaseCode/mast ...
- C++中的继承(1) 继承方式
1.继承与派生 继承是使代码可以复用的重要手段,也是面向对象程序设计的核心思想之一.简单的说,继承是指一个对象直接使用另一对象的属性和方法.继承呈现了 面向对象程序设 计的层次结构, 体现了 由简单 ...
- 【C++ Primer | 15】继承的构造函数
继承的构造函数 子类为完成基类初始化,在C++11之前,需要在初始化列表调用基类的构造函数,从而完成构造函数的传递.如果基类拥有多个构造函数,那么子类也需要实现多个与基类构造函数对应的构造函数. cl ...
随机推荐
- 538. Convert BST to Greater Tree
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original B ...
- openstack操作之一 命令行
在openstack环境中提供了多种操作虚拟机的方法,有最简单直接的dashborad界面,有不直观但高效的命令行,还有进阶版的postman调用openstack restfulapi和命令行中使用 ...
- [编织消息框架][网络IO模型]Netty Reactor
严格来讲Netty Reactor是一种设计模式,一听模式两字就知道了吧,套路哈哈 Reactor中文译为“反应堆”. 看图netty处理流程 1.netty server 至少有两组reactor. ...
- sql存储过程中使用 output
1.sql存储过程中使用 output CREATE PROCEDURE [dbo].[P_Max] @a int, -- 输入 @b int, -- 输入 @Returnc int output - ...
- Head First设计模式之迭代器模式
一.定义 提供一种方法顺序访问一个聚合对象中各个元素, 而又无须暴露该对象的内部表示: 主要解决:不同的方式来遍历整个整合对象. 何时使用:遍历一个聚合对象. 如何解决:把在元素之间游走的责任交给迭代 ...
- MVC实现(简单小例子)
Here I’ll demonstrate simple Spring MVC framework for building web applications. First thing first. ...
- Fiddler中设置断点修改Request和Response
Fiddler中设置断点修改Request Fiddler最强大的功能莫过于设置断点了,设置好断点后,你可以修改httpRequest 的任何信息包括host, cookie或者表单中的数据.设置断点 ...
- Mac下安装pymssql
需要先安装freetds 先用 brew list 查看已经安装的包 如果已经安装freetds,则使用 brew uninstall freetds先卸载 brew unlink freetds b ...
- dubbo filter链构建过程
public <T> Exporter<T> export(Invoker<T> invoker) throws RpcException { if (Consta ...
- angular4.0快速import依赖路径
快捷键:ALT + ENTER 直接import对应的依赖路径