Ear*_*rlz 8 c# inheritance constructor
我有类结构
abstract class Animal {
public Animal(){
//init stuff..
}
}
class Cat : Animal {
public Cat(bool is_keyboard) : base() //NOTE here
{
//other init stuff
}
}
Run Code Online (Sandbox Code Playgroud)
现在,看看注意到的那一行.如果你删除: base()然后它将编译没有错误.
为什么是这样?有没有办法禁用这种行为?
: base()如果您不添加任何其他内容(任何: base(...)或: this(...)),则存在隐含.要强制它显式,请将参数添加到基础构造函数.然后它不能隐含.
例如:
public Animal(string name) {...}
Run Code Online (Sandbox Code Playgroud)