类层层派生,在实例化的时候构造函数的调用顺序是怎样的? --从顶层基类开始向子类方向顺序调用无参构造.
默认构造(无参构造)和带参构造什么时候调用?--默认将从顶层父类的默认构造一直调用到当前类的默认构造.
下面是示例:
![](https://i-blog.csdnimg.cn/blog_migrate/2e235d0fe8c4430a39659743199970b5.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1f5e3af9bc11b47306983d1010ebadd3.png)
![](https://i-blog.csdnimg.cn/blog_migrate/7f15c6ca2ac2111bd44bbaecbea4a700.png)
![](https://i-blog.csdnimg.cn/blog_migrate/7f15c6ca2ac2111bd44bbaecbea4a700.png)
![](https://i-blog.csdnimg.cn/blog_migrate/7f15c6ca2ac2111bd44bbaecbea4a700.png)
![](https://i-blog.csdnimg.cn/blog_migrate/2e235d0fe8c4430a39659743199970b5.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/f8c08736ea3e049cea38f23ac94a31da.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/f8c08736ea3e049cea38f23ac94a31da.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/eb125a6c79b06a47cb505a3be0810c06.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/f8c08736ea3e049cea38f23ac94a31da.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/eb125a6c79b06a47cb505a3be0810c06.png)
![](https://i-blog.csdnimg.cn/blog_migrate/eb125a6c79b06a47cb505a3be0810c06.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/f8c08736ea3e049cea38f23ac94a31da.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/f8c08736ea3e049cea38f23ac94a31da.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/eb125a6c79b06a47cb505a3be0810c06.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/f8c08736ea3e049cea38f23ac94a31da.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/eb125a6c79b06a47cb505a3be0810c06.png)
![](https://i-blog.csdnimg.cn/blog_migrate/eb125a6c79b06a47cb505a3be0810c06.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/f8c08736ea3e049cea38f23ac94a31da.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/f8c08736ea3e049cea38f23ac94a31da.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/eb125a6c79b06a47cb505a3be0810c06.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/f8c08736ea3e049cea38f23ac94a31da.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/eb125a6c79b06a47cb505a3be0810c06.png)
![](https://i-blog.csdnimg.cn/blog_migrate/eb125a6c79b06a47cb505a3be0810c06.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/f8c08736ea3e049cea38f23ac94a31da.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/f8c08736ea3e049cea38f23ac94a31da.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/eb125a6c79b06a47cb505a3be0810c06.png)
![](https://i-blog.csdnimg.cn/blog_migrate/eb125a6c79b06a47cb505a3be0810c06.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1f5e3af9bc11b47306983d1010ebadd3.png)
![](https://i-blog.csdnimg.cn/blog_migrate/2e235d0fe8c4430a39659743199970b5.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/63087009de5dfc0201830d9fa2a8fae6.png)
![](https://i-blog.csdnimg.cn/blog_migrate/1f5e3af9bc11b47306983d1010ebadd3.png)
//1.构造函数继承的时候,先调用基类的构造函数方法,再调用自己的构造函数方法
//2.子类构造函数可以继承父类不同参数个数的构造函数
[csharp]
view plain
copy
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace 构造函数继承
- {
- //1.构造函数继承的时候,先调用基类的构造函数方法,再调用自己的构造函数方法
- //2.子类构造函数可以继承父类不同参数个数的构造函数
- class Class1
- {
- public Class1(string a, string b)
- {
- Console.WriteLine("Class1 2个参数的构造函数,a={0},b={1}", a, b);
- }
- }
- class Class2 : Class1
- {
- public Class2():base("class2_a","class2_b")
- {
- Console.WriteLine("Class2无参构造函数 继承自Class1的被调用");
- }
- }
- class Test
- {
- static void Main(string[] args)
- {
- Class2 class2 = new Class2();
- }
- }
- }
public class A
{
public A(int a)
{
Debug.Log(a);
}
}
public class B:A
{
public B(int b):base(b/3)
{
}
}
B b =new B(3);
输出1
B b =new B(3);
输出1
构造从base开始,析构则相反
class First{ ~First() { System.Diagnostics.Trace.WriteLine( "First's destructor is called."); }} class Second : First{ ~Second() { System.Diagnostics.Trace.WriteLine( "Second's destructor is called."); }} class Third : Second{ ~Third() { System.Diagnostics.Trace.WriteLine( "Third's destructor is called."); }} class TestDestructors{ static void Main() { Third t = new Third(); }} /* Output (to VS Output Window): Third's destructor is called. Second's destructor is called. First's destructor is called.*/