.Net C#语法 构造函数中this和base
来源:互联网 发布:linux 迅雷 编辑:程序博客网 时间:2024/06/10 06:19
类:
public class TestClassA { public TestClassA() { Console.WriteLine("我是在TestClassA中的TestClassA()构造函数中"); } public TestClassA(string a, string b) { Console.WriteLine("我是在TestClassA中的TestClassA(string a, string b)构造函数中"); } public TestClassA(string a) : this(a, "b") { Console.WriteLine("我是在TestClassA中的TestClassA(string a): this(a, \"b\")构造函数中"); } } public class TestClassB : TestClassA { public TestClassB() { Console.WriteLine("我是在TestClassB中的TestClassB()构造函数中"); } public TestClassB(string a, string b) : base(a, b) { Console.WriteLine("我是在TestClassB中的TestClassB(string a, string b): base(a, b)构造函数中"); } public TestClassB(string a) : this(a, "b") { Console.WriteLine("我是在TestClassB中的TestClassB(string a, string b): this(a, \"b\")构造函数中"); } }
实例:
Console.WriteLine("TestClassA Father1 = new TestClassA();运行结果:"); TestClassA Father1 = new TestClassA(); Console.WriteLine(); Console.WriteLine("TestClassA Father2 = new TestClassA(\"f2\", \"f2\");运行结果:"); TestClassA Father2 = new TestClassA("f2", "f2"); Console.WriteLine(); Console.WriteLine("TestClassA Father3 = new TestClassA(\"f3\");运行结果:"); TestClassA Father3 = new TestClassA("f3"); Console.WriteLine(); Console.WriteLine("TestClassB Son1 = new TestClassB();运行结果:"); TestClassB Son1 = new TestClassB(); Console.WriteLine(); Console.WriteLine("TestClassB Son2 = new TestClassB(\"s2\", \"s2\");运行结果:"); TestClassB Son2 = new TestClassB("s2", "s2"); Console.WriteLine(); Console.WriteLine("TestClassB Son3 = new TestClassB(\"s3\");运行结果:"); TestClassB Son3 = new TestClassB("s3"); Console.WriteLine();
结果:
分析:
this:调用的是本身,不能调用父类和子类的
base:调用父类的,不能调用本身的,但别人继承,可以调用
从中也可以得出另外个结果构造函数的运行过程 先从基类开始构造再到类本身
本博客内容有些来源于网络或书籍如果侵害到你的权益,请及时联系我(hch458458@vip.qq.com)
版权归nethch所有,转载请注明出处!
- .Net C#语法 构造函数中this和base
- .Net C#语法 构造函数中this和base
- C#构造函数里的base和this
- C#构造函数里的base和this的区别
- c# base和this在构造函数中的应用
- C#构造函数里的base和this的区别
- C#构造函数里的base和this的区别
- C#构造函数里的base和this的区别
- C#构造函数里的base和this的区别
- 构造函数中base和this的用法与区别
- 构造函数中base和this的用法与区别
- C#中base和this关键字
- C#中base和this关键字
- c#中this和base的区别
- C#中base和this关键字
- C#中base和this关键字
- C#中base和this关键字
- C# base和this
- hdu 4000 Fruit Ninja
- Android Intent Filter简单使用
- 2.3版本修改录像编码格式
- 高斯取整函数与Beatty定理
- 文件的多线程下载
- .Net C#语法 构造函数中this和base
- ext numberfield添加金额输入效果
- Debian使用入门(1) apt-cache
- 字符串匹配算法 Boyer-Moore
- POJ 2226 Muddy Fields 匈牙利算法/最小点覆盖
- 网站建设-如何才能设计好一个网站
- Linux 下值得使用的 5 个屏幕录像软件
- 类比推理每日练习及精解20110830
- GCC 编译命令笔记