作业帮 > 综合 > 作业

C# 如何定义构造函数

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/04/28 03:05:16
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;