首页 > 其他 > 详细

【转载】Derived Classes Do Not Inherit Constructors

时间:2014-03-11 16:26:57      阅读:448      评论:0      收藏:0      [点我收藏+]

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:

bubuko.com,布布扣
 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 }
bubuko.com,布布扣

【转载】Derived Classes Do Not Inherit Constructors,布布扣,bubuko.com

【转载】Derived Classes Do Not Inherit Constructors

原文:http://www.cnblogs.com/yuthreestone/p/3593601.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!