每天学一点,每天积累一点,进步就不止一点点!PS:好记性不如烂笔头,学会总结,学会思考~~~ ----要飞翔,必须靠自己!

灰太狼的梦想

好记性不如烂笔头,学会总结,学会思考~~~

C#构造函数

1.构造函数是一个特殊的方法,没有返回值类型,不能返回值,方法名称和类名一致。

2.构造函数的作用就是构建对象的属性值。

3.任何类的成员都不可能与类名称一样,除了构造函数与析构函数。

4.任何类都会默认提供一个无参的构造函数。但是如果手动添加了带参的构造函数,那么无参的构造函数,会自动消失,所以在创建类之后,立马为其添加一个无参的构造函数。

不添加的话,在下面的例子中会有问题。

5.构造函数本身也是一个方法,所以可以重载,但是不能像普通方法一样的调用,它只有两个场合可以使用

*创建对象new的时候

*构造函数之间的相互调用,同一个类的构造函数调用(使用this关键字);子类调用父类的构造函数(使用base关键字)。

*同一个类的构造函数的相互调用,将接收到的实参值再传递给另外一个构造函数。

*在new一个子类的构造函数的时候,先会去创建父类的构造函数,再来创建自己的构造函数

*构造函数不可以继承,因为构造函数就是构造每个对象自己的方法,但是构造函数是可以来调用的。

*父类在帮子类构造的时候,不能使用子类非公共成员,否则就循环依赖了。

*子类构造函数默认调用父类的无参构造函数,如果父类没有无参构造函数,就必须指明调用父类哪个构造函数。

*如果子类重写了父类的成员,那么this.成员。调用的是子类重写以后的。base.成员调用的依然是父类的成员。

 

当存在有参的构造函数之后,无参的构造函数自动消失了,必须要手动添加无参的构造函数。

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 
  7 namespace 属性的封装
  8 {
  9     class Teacher
 10     {
 11         /// <summary>
 12         /// 默认的无参构造函数
 13         /// </summary>
 14         public Teacher() { }
 15 
 16 
 17         /// <summary>
 18         /// 
 19         /// </summary>
 20         /// <param name="phone"></param>
 21         public Teacher(int age)
 22         {
 23             this.Age = age;
 24         }
 25 
 26         /// <summary>
 27         /// 构造函数
 28         /// </summary>
 29         /// <param name="name"></param>
 30         public Teacher(string name, string sex, int age):this(age)
 31         {
 32             this.Name = name;
 33             Sex = sex;
 34            // Age = age;
 35         }
 36 
 37         /// <summary>
 38         /// 
 39         /// </summary>
 40         /// <param name="name"></param>
 41         /// <param name="sex"></param>
 42         /// <param name="age"></param>
 43         /// <param name="phone"></param>
 44         /// <param name="subject"></param>
 45         public Teacher(string name, string sex, int age, string phone, string subject):this(name,sex,age)
 46         {
 47             //this.Name = name;
 48             //this.Sex = sex;
 49             //this.Age = age;
 50             this.Phone = phone;
 51             this.Subject = subject;
 52         }
 53 
 54        
 55        
 56        
 57 
 58         #region 工号
 59         /// <summary>
 60         /// 工号
 61         /// </summary>
 62         int id;
 63 
 64         public int Id
 65         {
 66             get { return id; }
 67             set { id = value; }
 68         } 
 69         #endregion
 70 
 71         #region 姓名
 72         /// <summary>
 73         /// 姓名
 74         /// </summary>
 75         string name;
 76 
 77         public string Name
 78         {
 79             get { return name; }
 80             set { name = value; }
 81         } 
 82         #endregion
 83 
 84         #region 性别
 85         /// <summary>
 86         /// 性别
 87         /// </summary>
 88         string sex;
 89 
 90         public string Sex
 91         {
 92             get { return sex; }
 93             set 
 94             {
 95                 if (value == "" || value == "")
 96                 {
 97                     sex = value;
 98                 }
 99                 else
100                 {
101                     sex = "人妖";
102                 }
103                 
104             }
105         } 
106         #endregion
107 
108         #region 年龄
109         /// <summary>
110         /// 年龄
111         /// </summary>
112         int age;
113 
114         public int Age
115         {
116             get { return age; }
117             set 
118             {
119                 if (value > 0 && value <= 100)
120                 {
121                     age = value;
122                 }
123                 else
124                 {
125                     age = 18;
126                 }
127                
128             }
129         } 
130         #endregion
131 
132         #region 课程
133         /// <summary>
134         /// 课程
135         /// </summary>
136         string subject;
137 
138         public string Subject
139         {
140             get { return subject; }
141             set { subject = value; }
142         } 
143         #endregion
144 
145         #region 电话号码
146         /// <summary>
147         /// 电话号码
148         /// </summary>
149         string phone;
150 
151         public string Phone
152         {
153             get { return phone; }
154             set { phone = value; }
155         } 
156         #endregion
157 
158         #region Show
159         /// <summary>
160         /// Show
161         /// </summary>
162         public void Show()
163         {
164             Console.WriteLine("大家好,我是你们的老师,我叫{0} ,我的性别是{1} ,年龄是{2},我教的课程是{3}", this.Name, this.Sex, this.Age, this.Subject);
165         }
166         
167         #endregion
168     }
169 }
Teacher类
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 属性的封装
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13          
14             //
15             Teacher t = new Teacher("ccc","sss",22);
16             t.Show();
17             Teacher t1 = new Teacher("fangfang", "girl", 22, "12234242342", "语文");
18             t1.Show();
19             Console.ReadKey();
20         }
21     }
22 }
Main函数

 

 

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 属性封装
{
    class Student:People
    {
        public Student()
        { }

        public Student(string name, int age)    
        {
            this.Name = name;
            this.Age = age; 
        }
        public Student(string name, string sex, int age):this(name,age)
        {
            //this.Name = name;
            this.Sex = sex;
            //this.Age = age; 
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 属性封装
{
    class Program
    {
        static void Main(string[] args)
        {
            //Student stu = new Student() 
            //{ 
            //  ID=1,
            //  Name="mike",
            //  Sex="nan",
            //  Age=233
            //};
            //stu.Show();
            Student stu1 = new Student("ccc", "nan", 12);
            Console.ReadKey();
        }
    }
}

这里测试的时候,是调用带有3个参数的构造函数,

    public Student(string name, string sex, int age):this(name,age)
        {
            //this.Name = name;
            this.Sex = sex;
            //this.Age = age; 
        }
执行到这个构造函数的时候,不会先进来这个构造函数的方法体,而是先去this里面,即调用带有两个参数的构造函数。
   public Student(string name, int age)    
        {
            this.Name = name;
            this.Age = age; 
        }

 

这里的aa是瞒天过海,我们调用的时候,    Teacher ta = new Teacher("ccc", "nan", 12);,传递的是,ccc,结果到了那,给偷工减料,传递aa了。。


 
posted @ 2015-05-17 22:34  灰太狼的梦想  阅读(530)  评论(0编辑  收藏  举报