继承中的构造函数的问题base,this的用法 Carl

先写一个父类:

 1 class Person
 2     {
 3        
 4         public string Name
 5         {
 6             get;
 7             set;
 8         }
 9         public int Age
10         {
11             get;
12             set;
13         }
14     }

在写一个继承这个父类的子类

 1 class Student : Person
 2     {
 3      public Student(string name, int age, double score)
 4        {
 5              this.Name = name;
 6             this.Age = age;
 7             this.Score = score;
 8         }
 9         public double Score
10         {
11             get;
12             set;
13         }
14     }

实例化子类并调用:

1 Student stu = new Student("张三", 18, 100);

编译运行成功!证明我们的写法是没有问题的!
这个时候我们修改下父类的代码:

 1     class Person
 2     {
 3         
 4         public Person(string name, int age)
 5         {
 6             this.Name = name;
 7             this.Age = age;
 8         }
 9 
10        
11         public string Name
12         {
13             get;
14             set;
15         }
16         public int Age
17         {
18             get;
19             set;
20         }
21     }

这个时候编译就会出错:提示缺少一个无参数的构造函数:
少什么我们就先加什么:那么我们就给父类增加一个无参数的构造函数

class Person
    {
     
        public Person()
        {
            Console.WriteLine("Person类中的无参数的构造函数。。。。");
        }

        public Person(string name, int age)
        {
            this.Name = name;
            this.Age = age;
        }
        public string Name
        {
            get;
            set;
        }
        public int Age
        {
            get;
            set;
        }
    }

我们在编译,提示编译成功!

 证明在调用子类的构造函数会默认去调用父类中的无参数的构造函数!继承的时候,构造函数不能被继承

但是当父类中不存在无参数的构造函数呢!

 1  class Person
 2     {
 3         
 4         public Person(string name, int age)
 5         {
 6             this.Name = name;
 7             this.Age = age;
 8         }
 9 
10             public string Name
11         {
12             get;
13             set;
14         }
15         public int Age
16         {
17             get;
18             set;
19         }
20     }

这个时候我们就可以使用base方法,在子类中制定要调用父类的构造函数,这样子类就不会默认的去调用父类中无参数的构造函数,编译也不会报错

class Student : Person
    {

        //不修改父类,而是在子类的构造函数后面通过:base(),显示的去调用父类的某个构造函数。
        public Student(string name, int age, double score)
            : base(name, age) //base的作用1:在子类中调用父类的构造函数。
        {
            // this.Name = name;
            //this.Age = age;
            this.Score = score;
        }
        public double Score
        {
            get;
            set;
        }
    }

上面讲了Base的用法,下面我们将下this的用法!

先看一段代码

 1  public class Person : MyClass
 2     {
 3         public Person(string name, int age, string email, double salary)
 4         {
 5             this.Name = name;
 6             this.Age = age;
 7             this.Email = email;
 8             this.Salary = salary;
 9         }
10 
11         public Person(string name)
12            
13         {
14              this.Name = name;
15         }
16         public Person(string name, int age)
17             
18         {
19             this.Name = name;
20             this.Age = age;
21         }
22         public Person(string name, string email)
23             
24         {
25             this.Name = name;
26             this.Email = email;
27         }
28 
29 
30         public string Name
31         {
32             get;
33             set;
34         }
35         public int Age
36         {
37             get;
38             set;
39         }
40         public string Email
41         {
42             get;
43             set;
44         }
45 
46         public double Salary
47         {
48             get;
49             set;
50         }
51     }

通过上面的代码你是不是发现一些问题:有很多重复的地方,一个类里面有很多种构造方法,如果每一个我们都给里面的参数赋值的话,显得非常的麻烦,通过this我们就能更好的优化这些代码,并且清晰明了

 1  public class Person : MyClass
 2     {
 3         public Person(string name, int age, string email, double salary)
 4         {
 5             this.Name = name;
 6             this.Age = age;
 7             this.Email = email;
 8             this.Salary = salary;
 9         }
10 
11         //this作用1:在当前类的构造函数后面通过:this来调用当前类自己的其他构造函数。
12         public Person(string name)
13             : this(name, 0, null, 0)
14         {
15             // this.Name = name;
16         }
17         public Person(string name, int age)
18             : this(name, age, null, 0)
19         {
20             //this.Name = name;
21             //this.Age = age;
22         }
23         public Person(string name, string email)
24             : this(name, 0, email, 0)
25         {
26             //this.Name = name;
27             //this.Email = email;
28         }
29 
30 
31         public string Name
32         {
33             get;
34             set;
35         }
36         public int Age
37         {
38             get;
39             set;
40         }
41         public string Email
42         {
43             get;
44             set;
45         }
46 
47         public double Salary
48         {
49             get;
50             set;
51         }
52     }

  当然this还有很多其它的用法,不仅仅是这一种 ,不同的场合有不同的含义!本文仅介绍在构造函数中的用法!

原文地址:https://www.cnblogs.com/sc0791/p/2645966.html