A derived class inherits all of the members of a base class except for its constructors.
You must define a constructor in a derived classunlessthe base class has defined a default (parameterless) constructor. If you don’t define a constructor in the derived class, the default constructor in the base class is called implicitly.
When you define a constructor in a derived class, you can call a constructor in the base class by using thebasekeyWord.
Here’s an example:
1 public class Dog 2 { 3 public string Name { get; set; } 4 public int Age { get; set; } 5 6 public Dog(string name, int age) 7 { 8 Name = name; 9 Age = age;10 }11 }12 13 public class Terrier : Dog14 {15 public string Attitude { get; set; }16 17 // Call the Name/Age constructor in the base class18 public Terrier(string name, int age, string attitude)19 : base(name, age)20 {21 Attitude = attitude;22 }23 }
原文地址:#330 - Derived Classes Do Not Inherit Constructors
新闻热点
疑难解答