C#构造函数里的base和this的区别
来源:互联网 发布:自带软件删除 编辑:程序博客网 时间:2024/05/16 19:52
用法一:
父类的构造函数总是在子类之前执行的。既先初始化静态构造函数,后初始化子类构造函数。
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是调用基类的有参数构造函数。
阅读全文
0 0
- C#构造函数里的base和this的区别
- C#构造函数里的base和this的区别
- C#构造函数里的base和this的区别
- C#构造函数里的base和this的区别
- C#构造函数里的base和this的区别
- C#构造函数里的base和this
- 构造函数中base和this的用法与区别
- 构造函数中base和this的用法与区别
- c#中this和base的区别
- C# base和this的区别
- base 和this的区别
- .Net C#语法 构造函数中this和base
- .Net C#语法 构造函数中this和base
- c# base和this在构造函数中的应用
- C#中base跟this的区别
- C#中this与base的区别
- C++ 和 C# 构造函数的区别
- C++和C#构造函数的区别
- 解题报告:Codeforces Round #432 (Div. 1) D. Tournament Construction (DP+构造)
- 学生管理系统总结(一)
- C-一个通用查找函数
- 数据挖掘领域重要会议与期刊情况
- 新博客地址
- C#构造函数里的base和this的区别
- 路由转发实验
- 9-7考试总结
- 土圭垚㙓数学课(二)视锥体八个顶点的计算方法
- Android Drawable Resources 之 Shape Drawable 使用详解
- 「网络流 24 题」圆桌聚餐
- Ubuntu16.04 安装谷歌浏览器并配置常用插件与扩展
- 机器学习——线性回归(梯度下降和正规方程)
- 简单背包练习——0/1背包