`

非静态构造函数笔记

    博客分类:
  • C#
阅读更多
(1)不带参数的构造函数称为“默认构造函数”。无论何时,只要使用 new 运算符实例化对象,并且不为 new 提供任何参数,就会调用默认构造函数。
/***********************************************************************
 * 调用默认的构造函数 
************************************************************************/
using System;
class A
{
    public A() { }//可省略,系统会自动添加
}
class C
{
    static void Main(string[] args)
    {
    //此时,系统自动调用A(),初始化A的变量 
        A a = new A();
    }
}



(2)派生类可以显示调用基类的构造函数。用户不能显示调用构造函数
/***********************************************************************
 * 派生类可以显示调用基类的构造函数。用户不能显示调用构造函数 
************************************************************************/
using System;
class A
{
    public A() { }
}
class B:A 
{
    public B():base (){}//显示调用基类的构造函数
}
class C
{
    static void Main(string[] args)
    {
        A a = new A();
        //错误,用户不能显示调用构造函数。构造函数是由系统根据实例化时的参数自动调用的。 
        a.A();
    }
}


(3)先执行基类的构造函数,再执行派生类的构造函数。
/*********************************************************************** 
 * 实例化时先执行基类A的构造函数,再行B类的构造函数
************************************************************************/
using System;
class A
{
    public A() { Console.WriteLine("这是A的构造函数"); }  
}
class B:A 
{
    public B() { Console.WriteLine("这是B的构造函数");  }
}
class C
{
    static void Main(string[] args)
    {        
       B b = new B ();        
    }
}

(4)在派生类中,如果不使用 base 关键字来显式调用基类构造函数,则将隐式调用默认构造函数(如果有的话)。
/***********************************************************************
 * 在派生类中,如果不使用 base 关键字来显式调用基类构造函数,则将隐式调用默认构造函数。 
************************************************************************/
using System;
class A
{
    public A() { Console.WriteLine("这是A的构造函数"); }
    public A(int i) { Console.WriteLine("这是A的有参构造函数"); }
}
class B:A 
{
    public B() { Console.WriteLine("这是B的构造函数");  }
}
class C
{
    static void Main(string[] args)
    {
        //派生类B中,没有指定基类A的构造函数。此时先调用A的默认构造函数,再调用B的构造函数
       B b = new B ();        
    }
}


(5)如果在基类中有自己写的构造函数,系统就不会调用基类默认的构造函数,若在派生类中没有显式的指定基类的构造函数,此时就会出错,需要在基类中,添加基类默认的构造函数。

/***********************************************************************
 * 基类A中有自己写的有参构造函数(系统不再提供默认的构造函数),
 * 派生类B中没有指定A的构造函数,此时系统会寻找基类A的默认构造函数,因此会
 * 出错。(提示:A中不包含0个参数的构造函数)
 * 
************************************************************************/
using System;
class A
{    
    public A(int i) { Console.WriteLine("这是A的有参构造函数"); }
}
class B:A 
{
    public B() { Console.WriteLine("这是B的构造函数");  }
}
class C
{
    static void Main(string[] args)
    {
        //出错,A中不包含0个参数的构造函数
       B b = new B ();        
    }
}



/*********************************************************************** 
 * 派生类B中指定了基类A的构造函数,不会出错
************************************************************************/
using System;
class A
{    
    public A(int i) { Console.WriteLine("这是A的有参构造函数"); }
}
class B:A 
{
    public B() :base(1) { Console.WriteLine("这是B的构造函数");  }
}
class C
{
    static void Main(string[] args)
    {
        //派生类B中指定了基类A的构造函数,不会出错
       B b = new B ();        
    }
}



/*********************************************************************** 
 * 派生类中不指定基类的构造函数而为基类添加默认的构造函数,不会出错
************************************************************************/
using System;
class A
{
    public A() { Console.WriteLine("这是A的构造函数"); }
    public A(int i) { Console.WriteLine("这是A的有参构造函数"); }
}
class B:A 
{
    public B() { Console.WriteLine("这是B的构造函数");  }
}
class C
{
    static void Main(string[] args)
    {
        //派生类B中指定了基类A的构造函数,但为基类添加默认的构造函数,不会出错
       B b = new B ();        
    }
}


可以得到的结论是   先调用父类的构造函数进行初始化,再调用继承类的构造函数,如果没有在继承类中指明父类的构造函数,则默认调用父类中没有参数的构造函数,然后调用继承类的构造函数

(6)构造函数可以使用 this 关键字调用同一对象中的另一构造函数
/*********************************************************************** 
 * 使用this可以调用同一类中另一个构造函数
************************************************************************/
using System;
class A
{
    public A():this("abc") { Console.WriteLine("这是A无参数的构造函数"); }
    public A(string s) { Console.WriteLine("这是A有一个参数的构造函数"); }  
}
class B:A 
{
    public B() { Console.WriteLine("这是B的构造函数");  }
}
class C
{
    static void Main(string[] args)
    {
        //先执行A中有一个参数的构造函数,再执行A中无参数的构造函数,最后执行B的构造函数
       B b = new B ();        
    }
}









分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics