C# 如何定义构造函数

如题所述

定义:
通用语言运行时CLR要求每个类都有一个构造函数。构造函数是一个有特殊用途的方法,第一次引用时会初始化类或类实例。

分类:

实例构造函数(instance)、私有构造函数(private,实例构造函数的一种特殊情况)和静态构造函数(static)。

构造函数没有返回数据类型,且增加了一个initializer(初始化调用)的选项,其余和一般方法没有区别,不过还有一些约束

1,构造函数必须与类名相同

2,通过initializer在构造函数体前调用基类或者本类的另一个构造函数的方法

a,base(argument list) -调用基类构造函数

b,this(argument list) 调用这个类的另一个构造函数

3,如果类没有写构造函数,编译器将会给类创造一个无参的构造函数

实例化类的时候其实就是调用了构造函数:

如 :

Test t=new Test();//调用默认的空构造函数

Test tt=new Test(test);//调用了有一个参数的构造函数

私有构造函数:

我们知道对类成员用private修饰时候,是为了不允许在类外的范围这个成员,

那么,用private修饰构造函数的时候,是为禁止实例化这个类的功能,如果一个类只提供静态的方法和字段,就可以使用私有构造函数,注:使用了私有构造函数不能被也不会被继承,不过在类中可以对自己进行实例化~可以提供一个工厂方法创建对象

静态构造函数:

主要作用是初始化静态类成员,

限制:不能有参数,不能重载(一个类只能有一个静态构造函数) 性必须是private,不能调用其他构造函数,只 能类中的静态成员,注意和默认的无参数的构造函数进行区分

下面的代码是我练习的时候写的简单的使用例子:

using System;

using System.Collections.Generic;

using System.Text;

namespace ConsoleApplication1

{

class A

{

public A(){} //这个构造函数编译器会自己添加,写和不写一样

private string _firstName;

private string _lastName;

public A(string firstName, string lastName) {

this._firstName = firstName;

this._lastName = lastName;

}

public string FirstName

{

set

{

_firstName = value;

}

get

{

return _firstName;

}

}

public string LastName

{

get { return _lastName; }

set { _lastName = value; }

}

public override string ToString() //重写了object的ToString()方法

{

return My Name is +_firstName+ + _lastName;

}

};

class B : A

{

private string title;

public string Titel

{

get { return title; }

set { title = value; }

}

public B(string firstName, string lastName, string title):base(firstName,lastName)// 这里调用了基类的构造函数

{

this.title = title;

}

public override string ToString()

{

return base.ToString()+,And my title is +this.title;

}

}

class C:A

{

private string _title;

public string Titel

{

get { return _title; }

set { _title = value; }

}

private string _level;

public string Level

{

get { return _level; }

set { _level = value; }

}

public C(string firstName, string lastNamt, string title)

: base(firstName, lastNamt)

{

this._title = title;

}

public C(string firstName, string lastName, string title, string level)

: this(firstName, lastName, title)//调用了自己的另一个构造函数

{

this._level = level;

}

public override string ToString()

{

if(this._level==null){

return base.ToString()+, And My Title is +this._title;

}else{

return base.ToString()+, My Title is +this._title +, And My Level is +this._level;

}

}

}

//私有构造函数

class D

{

private D()

{

}

//后补充

public staic D CreateD=new D();

private static string _name;

public static string Name

{

get { return _name; }

set { _name = value; }

}

private static int _age;

public static int Age

{

get { return _age; }

set { _age = value; }

}

public static new string ToString()

{

return My Name is + _name + , and I;m + _age.ToString();

}

}

//静态构造函数类

class E

{

private string name;

public string Name

{

get { return name; }

set { name = value; }

}

private int age;

public int Age

{

get { return age; }

set { age = value; }

}

private string title;

public string Title

{

get { return title; }

set { title = value; }

}

private static int i;

static E()

{

Console.WriteLine(Static 构造函数+i);

}

public E()

{

i += 1;

Console.WriteLine( 普通 构造函数 + i);

}

public E(string name, int age)

{

this.name = name;

this.age = age;
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-10-17
构造函数名与类名相同,不返回任何值 ,主要作用是给字段赋初始值;

public class Teacher{public <strong>Teacher</strong>(){}//无参数的构造函数 函数名Teacher 要和类名 相同 publlc Teacher(string names)//有参数的构造函数,给Teacher 类中的字段name 赋初始值{name=names;}string name;//Student 类中的字段public string Name//封装字段{get{return name;}set{name=value;}}}
第2个回答  2013-10-17
public class Student{
//无参数构造

public Student(){
}
public Student(string name){
}
}
第3个回答  2013-10-17
VS中构造函数没有快捷键。只能手动敲出来。(注意的是,最好要重载这个构造函数,一个无参,一个有参的,因为在类中,没有系统默认的无参构造早函数,如果在结构中可以省去无参的构造函数)。
VS中虽然没有构造函数的快捷键,但是对字段的封装有快捷键。(ctrl R E)或者在写字段时写打上PROP然后按2次TAB键,直接可以把字段和属性的格式打出来了,然后改下字段和属性的类型名称即可。

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网