継承中のコンストラクション関数の問題-base,thisの使い方

25146 ワード

親を先に書きます.
 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;

        }

    }

 
ベースの使い方を説明しましたが、次は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     }

上のコードを通じてあなたはいくつかの問題を発見しますか:多くの重複の地方があって、1つのクラスの中に多くの構造の方法があって、もしすべての私达がすべて中のパラメータに値を割り当てるならば、とても面倒に見えて、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には他にもいろいろな使い方がありますが、これだけでなく、場面によって意味が違います!この文書では、コンストラクション関数での使用方法についてのみ説明します.