2017-01-14 17 views
-3

":this()"が参照するものを説明してください。私は代わりに "base"を使用します。 "this"にカーソルを移動すると、別のコンストラクターが表示されます。 また、 "base"で置き換えると、 "objectには1つの引数を取るコンストラクタが含まれていません"というエラーが返されます。"this"はコンストラクタごとに何を参照していますか?

class Employee 
    { 
     private string name; 
     private string surname; 
     private DateTime birthday; 
     private double height; 
     private double salary; 
     public string Name { get { return name; } set { if (name != value) { name = value; } } } 
     public string Surname { get { return surname; } set { if (surname != value) { surname = value; } } } 
     public DateTime Birthday { get { return birthday; } set { if (birthday != value) { birthday = value; } } } 
     public double Height { get { return height; } set { if (height != value) { height = value; } } } 
     public double Salary { get { return salary; } set { if (salary != value) { salary = value; } } } 
     public string Full { get { return name + " " + surname; } } 
     public Employee() { 
      name = "Mina"; 
      surname = "Babayeva"; 
      birthday = DateTime.Now.AddYears(-19); 
      height=175; 
      salary = 3500; 
     } 

     public Employee(string name, string surname, DateTime birthday, double height, double salary) : this(name, surname) 
     { 
      this.birthday = birthday; 
      this.height = height; 
      this.salary = salary; 
     } 
     public Employee(string name) : this() 
     { 
      this.name = name; 
     } 
     public Employee(string name, string surname) : this(name) 
     { 
      this.surname = surname; 
     } 


     public void Print() 
     { 
      Console.WriteLine("Name is {0}\nSurname is {1}\nHeight is {2}\nBirthday is {3}\nSalary is {4}", 
       this.Name, this.Surname, this.Height, this.Birthday.ToString("dd/MM/yyyy"), this.Salary); 
     } 


//////////////////////////////// 
Employee a = new Employee(); 
      Employee b = new Employee("Adil", "Babayev"); 
      Employee c = new Employee("Zumrud","Babayeva",DateTime.Now.AddYears(-52),167,1000000); 
      Employee e = new Employee("Hanna"); 


      a.Print(); 
      Console.WriteLine(); 
      b.Print(); 
      Console.WriteLine(); 
      c.Print(); 
      Console.WriteLine(); 
      e.Print(); 
      Console.WriteLine(); 

答えて

0

いつでも、あなたは実際にオブジェクトの単一コピーとしてインスタンスを作成しています。 このキーワードはそのインスタンスです。

例クラスアップル:オレンジ{}

パブリックアップル() :この(){} ここ。 Appleのインスタンスへの参照です。

クラスコンストラクタでbaseを呼び出すときは、派生クラスのコンストラクタを呼び出しています。 公開Apple() :base(){}

は、Appleクラスではなくオレンジクラスのコンストラクタを返します。 2人の巨大な違い!

C#コンストラクタチェインを参照してください。 MSDNには、あなたの探求に関する素晴らしい例とドキュメントがあります。
幸運な私の友人

種類親切。

+1

この場合、構文と書式を再度確認してください。 – Abion47

0

ここで、thisは、現在のクラスのコンストラクタを参照します。 baseは、ベース/継承クラスのコンストラクタを参照します。そうではありません、彼らは交換できません。

さらに詳しい情報:あなたはクラスのコンストラクタを作成MSDN Using Constructors

関連する問題