构造函数

  类层层派生,在实例化的时候构造函数的调用顺序是怎样的? --从顶层基类开始向子类方向顺序调用无参构造.
默认构造(无参构造)和带参构造什么时候调用?--默认将从顶层父类的默认构造一直调用到当前类的默认构造.
下面是示例:
/*--===------------------------------------------===---
作者:许明会
日期:类的派生和构造函数间的关系,调用层次及实现
日期:2008年1月18日 17:30:43
若希望类能够有派生类,必须为其实现默认构造函数.
若类没有实现带参构造,编译器将自动创建默认构造函数.
若类实现了带参构造,则编译器不会自动生成默认构造.
--===------------------------------------------===---
*/

using  System;

namespace  xumh
{
    
public class MyClass
    
{
        
public MyClass()
        
{            
            Console.WriteLine(
"MyClass:默认构造函数");
        }

        
public MyClass(int a, int b)
        
{
            Console.WriteLine(
"MyClass带参构造:a={0}, b={1}.", a, b);
        }

    }

    
    
public class MyClass2 : MyClass
    
{
        
public MyClass2()
        
{
            Console.WriteLine(
"MyClass2:默认构造函数");
        }

        
public MyClass2(int a, int b)
        
{
            Console.WriteLine(
"MyClass2带参构造:a={0}, b={1}.", a, b);
        }

    }


    
public class MyClass3 : MyClass2
    
{

        
public MyClass3()
        
{
            Console.WriteLine(
"MyClass3:默认构造函数");
        }

        
public MyClass3(int a, int b)
        
{
            Console.WriteLine(
"MyClass3带参构造:a={0}, b={1}.", a, b);
        }

    }


    
public class runMyApp
    
{
        
static void Main()
        
{
            MyClass3 my 
= new MyClass3(3,4);
        }

    }

}

/*--===------------------------------------------===---
输出如下:
MyClass:默认构造函数
MyClass2:默认构造函数
MyClass3带参构造:a=3, b=4.
--===------------------------------------------===---
*/




 



//1.构造函数继承的时候,先调用基类的构造函数方法,再调用自己的构造函数方法

//2.子类构造函数可以继承父类不同参数个数的构造函数

[csharp]  view plain  copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace 构造函数继承  
  7. {  
  8.     //1.构造函数继承的时候,先调用基类的构造函数方法,再调用自己的构造函数方法  
  9.     //2.子类构造函数可以继承父类不同参数个数的构造函数  
  10.     class Class1  
  11.     {  
  12.         public Class1(string a, string b)  
  13.         {  
  14.             Console.WriteLine("Class1 2个参数的构造函数,a={0},b={1}", a, b);  
  15.         }  
  16.     }  
  17.   
  18.     class Class2 : Class1  
  19.     {  
  20.         public Class2():base("class2_a","class2_b")  
  21.         {  
  22.             Console.WriteLine("Class2无参构造函数 继承自Class1的被调用");  
  23.         }  
  24.     }  
  25.   
  26.     class Test  
  27.     {  
  28.         static void Main(string[] args)  
  29.         {  
  30.             Class2 class2 = new Class2();  
  31.         }  
  32.     }  
  33. }  




    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

 

构造从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.*/
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值