【转载】#330 - Derived Classes Do Not Inherit Constructors
A derived class inherits all of the members of a base class except for its constructors.
You must define a constructor in a derived class unless the 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 the base keyword.
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 : Dog 14 { 15 public string Attitude { get; set; } 16 17 // Call the Name/Age constructor in the base class 18 public Terrier(string name, int age, string attitude) 19 : base(name, age) 20 { 21 Attitude = attitude; 22 } 23 }
posted on 2014-03-11 12:09 yuthreestone 阅读(192) 评论(0) 编辑 收藏 举报