为什么在 C# 中不继承构造函数?
问题描述:
我猜我不明白关于 C# 继承的一些非常基础的东西.有没有大神指教一下?
I'm guessing there's something really basic about C# inheritance that I don't understand. Would someone please enlighten me?
答
有时,在子类化时,您想限制创建类实例所需的条件.
Sometimes, when subclassing, you want to restrict the conditions required to create an instance of the class.
让我举个例子.如果类确实继承了它们的超类构造函数,则所有类都将具有来自 Object
的无参数构造函数.显然这是不正确的.
Let me give you an example. If classes did inherit their superclass constructors, all classes would have the parameterless constructor from Object
. Obviously that's not correct.