父类:
class Person { public Person() { Console.WriteLine("========================"); } public Person(string name, int age, string email) { this.Name = name; this.Age = age; this.Email = email; } public string Name { get; set; } public int Age { get; set; } public string Email { get; set; } }
子类:
class Teacher : Person { public Teacher(string name, int age, string email, double salary) : base(name, age, email) { //this.Name = name; //this.Age = age; //this.Email = email; this.Salary = salary; } public double Salary { get; set; } }
这样是调用父类中第二个有参的构造函数,如果去掉Base默认调用无参构造函数
注意执行顺序是:先调用父类的构造函数,然后再执行子类自己的构造函数。
时间: 2024-10-19 10:44:28