.Net C#语法 构造函数中this和base
2012-09-27 15:43
453 查看
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:调用父类的,不能调用本身的,但别人继承,可以调用
从中也可以得出另外个结果构造函数的运行过程 先从基类开始构造再到类本身
{
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:调用父类的,不能调用本身的,但别人继承,可以调用
从中也可以得出另外个结果构造函数的运行过程 先从基类开始构造再到类本身
相关文章推荐
- .Net C#语法 构造函数中this和base
- .Net C#语法 构造函数中this和base
- c#语法糖代码——继承中构造函数的问题,this&base比较
- C# 语法练习(11): 类[三] - 构造函数、析构函数、base、this
- c# base和this在构造函数中的应用
- c# base和this在构造函数中的应用
- c#学习-base和this在构造函数中的应用
- .NET/C# 类构造函数中this的用法
- .Net C#语法 构造函数与静态构造函数
- C#语法参考,new ,base,this,overide,virtual关键字,日常学习笔记。
- 【C#】C#构造函数中:base和:this的区别
- .NET/C# 类构造函数中this的用法 (转)
- [转]C#和VB.net语法对比图
- C# 中 this 和 base 的用法
- VB.Net与C# 的语法比较
- C# ASP.NET B/S模式下,采用lock语法 实现多用户并发产生不重复递增单号的一种解决方法技术参考
- C#.net中的using语法, SQl连接数据库
- [你必须知道的.NET] 第六回:深入浅出关键字---base和this
- 构造函数带有this和base的作用
- [你必须知道的.NET] 第六回:深入浅出关键字---base和this (转载)