c#语言基础(3)----类的构造函数

无意中使用构造函数发生了一些错误,补充下基础的东西,看来编程的路才开始,很长~

定义:

       通用语言运行时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,不能调用其他构造函数,只 能访问类中的静态成员,注意和默认的无参数的构造函数进行区分

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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace ConsoleApplication1
  5. {
  6.     class A
  7.     {
  8.         public A(){} //这个构造函数编译器会自己添加,写和不写一样
  9.         private string _firstName;
  10.         private string _lastName;
  11.         public A(string firstName, string lastName) {
  12.             this._firstName = firstName;
  13.             this._lastName = lastName;
  14.         }
  15.         public string FirstName
  16.         {
  17.             set
  18.             {
  19.                 _firstName = value;
  20.             }
  21.             get
  22.             {
  23.                 return _firstName;
  24.             }
  25.         }
  26.       
  27.         public string LastName
  28.         {
  29.             get { return _lastName; }
  30.             set { _lastName = value; }
  31.         }
  32.         public override string ToString()  //重写了object的ToString()方法
  33.         {
  34.             return "My  Name is "+_firstName+" " + _lastName;
  35.         }
  36.         
  37.     };
  38.     class B : A
  39.     {
  40.         private string title;
  41.         public string Titel
  42.         {
  43.             get { return title; }
  44.             set { title = value; }
  45.         }
  46.         public B(string firstName, string lastName, string title):base(firstName,lastName)// 这里调用了基类的构造函数
  47.         {
  48.             this.title = title;
  49.         }
  50.         public override string ToString()
  51.         {
  52.             return base.ToString()+",And my title is "+this.title;
  53.         }
  54.     }
  55.     class C:A
  56.     {
  57.         private string  _title;
  58.         public string  Titel
  59.         {
  60.             get { return _title; }
  61.             set { _title = value; }
  62.         }
  63.         private string _level;
  64.         public string Level
  65.         {
  66.             get { return _level; }
  67.             set { _level = value; }
  68.         }
  69.         public C(string firstName, string lastNamt, string title)
  70.             : base(firstName, lastNamt)
  71.         {
  72.             this._title = title;
  73.         }
  74.         public C(string firstName, string lastName, string title, string level)
  75.             : this(firstName, lastName, title)//调用了自己的另一个构造函数
  76.         {
  77.             this._level = level;
  78.         }
  79.         public override string ToString()
  80.         {
  81.             if(this._level==null){
  82.             return base.ToString()+", And My Title is "+this._title;
  83.             }else{
  84.                 return base.ToString()+", My Title is  "+this._title +", And My Level is  " +this._level;
  85.             }
  86.         }
  87.     }
  88. //私有构造函数
  89.     class D
  90.     {
  91.         private D()
  92.         {
  93.         }
  94.         //后补充
  95.         public staic D CreateD=new D();
  96.         private static string _name;
  97.         public static string Name
  98.         {
  99.             get { return _name; }
  100.             set { _name = value; }
  101.         }
  102.         private static int _age;
  103.         public static int Age
  104.         {
  105.             get { return _age; }
  106.             set { _age = value; }
  107.         }
  108.         public static new string ToString()
  109.         {
  110.             return "My Name is " + _name + ", and I;m " + _age.ToString();
  111.         }
  112.     }
  113. //静态构造函数类
  114.     class E
  115.     {
  116.         private string name;
  117.         public string Name
  118.         {
  119.             get { return name; }
  120.             set { name = value; }
  121.         }
  122.         private int age;
  123.         public int Age
  124.         {
  125.             get { return age; }
  126.             set { age = value; }
  127.         }
  128.         private string title;
  129.         public string Title
  130.         {
  131.             get { return title; }
  132.             set { title = value; }
  133.         }
  134.         private static int i;
  135.     
  136.         static E()
  137.         {
  138.             Console.WriteLine("Static 构造函数"+i);
  139.         }
  140.         public E()
  141.         {
  142.             i += 1;
  143.             Console.WriteLine(" 普通 构造函数" + i);
  144.         }
  145.         public E(string name, int age)
  146.         {
  147.             this.name = name;
  148.             this.age = age;
  149.         }
  150.         public E(string name, int age, string title):this(name,age)
  151.         {
  152.             this.title = title;
  153.         }
  154.         public override string ToString()
  155.         {
  156.             
  157.             return "My Name is " +name +",I'm "+age +"----No("+i+")";
  158.         }
  159.     }
  160. }
测试:

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace ConsoleApplication1
  5. {
  6.     class Test
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             A a = new A("li""si");
  11.             Console.WriteLine(a.ToString());
  12.             Console.ReadKey();
  13.             B b = new B("li""si""Code");
  14.             Console.WriteLine(b.ToString());
  15.             Console.ReadKey();
  16.             C c1 = new C("LI""si""Code");
  17.             Console.WriteLine(c1.ToString());
  18.             Console.ReadKey();
  19.             C c2 = new C("Li""si""Code","500");
  20.             Console.WriteLine(c2.ToString());
  21.             Console.ReadKey();
  22.             D.Name = "Li Si";
  23.             D.Age = 23;
  24.             Console.WriteLine(D.ToString());
  25.             Console.ReadKey();
  26.             E e1 = new E();
  27.             E e2=new E();
  28.             E e3=new E();
  29.             Console.ReadKey();
  30.             D d=new D();//这个会报错,
  31.             D  d=D.CreateD; 这个正确
  32.         }
  33.     };
  34.     
  35. }
结果就不贴了~主要是不会上传图片的 hoho
就记录这些了~


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值