首页 > 学院 > 开发设计 > 正文

【转载】#330

2019-11-17 03:18:35
字体:
来源:转载
供稿:网友

【转载】#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 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


上一篇:C# 解析 Json数据

下一篇:【转载】#324

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表