【转载】#330

王朝学院·作者佚名  2016-05-16
窄屏简体版  字體: 小  |  中  |  大  |  超大  

【转载】#330 - Derived Classes Do Not Inherit ConstructorsA 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

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航