无意中使用构造函数发生了一些错误,补充下基础的东西,看来编程的路才开始,很长~
测试:
结果就不贴了~主要是不会上传图片的 hoho
定义:
通用语言运行时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;
- }
- public E(string name, int age, string title):this(name,age)
- {
- this.title = title;
- }
- public override string ToString()
- {
- return "My Name is " +name +",I'm "+age +"----No("+i+")";
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace ConsoleApplication1
- {
- class Test
- {
- static void Main(string[] args)
- {
- A a = new A("li", "si");
- Console.WriteLine(a.ToString());
- Console.ReadKey();
- B b = new B("li", "si", "Code");
- Console.WriteLine(b.ToString());
- Console.ReadKey();
- C c1 = new C("LI", "si", "Code");
- Console.WriteLine(c1.ToString());
- Console.ReadKey();
- C c2 = new C("Li", "si", "Code","500");
- Console.WriteLine(c2.ToString());
- Console.ReadKey();
- D.Name = "Li Si";
- D.Age = 23;
- Console.WriteLine(D.ToString());
- Console.ReadKey();
- E e1 = new E();
- E e2=new E();
- E e3=new E();
- Console.ReadKey();
- D d=new D();//这个会报错,
- D d=D.CreateD; 这个正确
- }
- };
- }
就记录这些了~