【发布时间】:2014-03-28 03:55:48
【问题描述】:
我有一个非常简单直接的问题。 调用类的另一个构造函数以及该类的基本构造函数的标准化方法或正确方法是什么? 我知道第二个示例不起作用。以第三种方式来做这件事似乎有点骇人听闻。那么设计 C# 的人期望用户这样做的方式是什么?
例如:
public class Person
{
private int _id;
private string _name;
public Person()
{
_id = 0;
}
public Person(string name)
{
_name = name;
}
}
// Example 1
public class Engineer : Person
{
private int _numOfProblems;
public Engineer() : base()
{
_numOfProblems = 0;
}
public Engineer(string name) : this(), base(name)
{
}
}
// Example 2
public class Engineer : Person
{
private int _numOfProblems;
public Engineer() : base()
{
InitializeEngineer();
}
public Engineer(string name) : base(name)
{
InitializeEngineer();
}
private void InitializeEngineer()
{
_numOfProblems = 0;
}
}
【问题讨论】:
-
dna 是在哪里定义的?
-
已编辑,不是问题的重点。
标签: c# inheritance constructor