为什么不需要base()构造函数? [英] Why is the base() constructor not necessary?
本文介绍了为什么不需要base()构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类结构,像
abstract class Animal {
public Animal(){
//init stuff..
}
}
class Cat : Animal {
public Cat(bool is_keyboard) : base() //NOTE here
{
//other init stuff
}
}
现在,看看注释行。如果你删除:base()
,那么它会编译没有错误。
Now then, look at the noted line. If you remove : base()
then it will compile without an error.
为什么?有没有办法禁用此行为?
Why is this? Is there a way to disable this behavior?
推荐答案
有一个隐式:base code>如果你不添加任何东西(任何
:base(...)
或:this(...) / code>)。要强制它是显式的,添加一个参数到基础构造函数。
There is an implicit : base()
if you don't add anything else (any : base(...)
or : this(...)
). To force it to be explicit, add a parameter to the base-constructor(s). Then it can't be implicit.
例如:
public Animal(string name) {...}
这篇关于为什么不需要base()构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文