“公众”的相关性抽象类中的构造函数
抽象类中的 public
构造函数是否有任何相关性? 我想不出任何可能的使用方法,在这种情况下,编译器不应该将其视为错误(C#,不确定其他语言是否允许)。
示例代码:
internal abstract class Vehicle
{
public Vehicle()
{
}
}
C# 编译器允许编译此代码,但我无法从外部调用此构造函数。它只能从派生类中调用。
那么它不应该只允许 protected
和 private
修饰符吗?
Is there any relevance of a public
constructor in an abstract class?
I can not think of any possible way to use it, in that case shouldn't it be treated as error by compiler (C#, not sure if other languages allow that).
Sample Code:
internal abstract class Vehicle
{
public Vehicle()
{
}
}
The C# compiler allows this code to compile, while there is no way I can call this contructor from the outside world. It can be called from derived classes only.
So shouldn't it allow protected
and private
modifiers only?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有理由为抽象类提供公共构造函数。我认为编译器不抱怨的原因很简单,他们只是没有花时间讨论这一点,因为它是公共的还是受保护的并不重要。
There's no reason for a public constructor for an abstract class. I'd assume that the reason that the compiler doesn't complain is as simple that they just didn't spend time covering that since it really doesn't matter if it's public or protected.
在抽象类中,对于实例构造函数,修饰符
public
、protectedinternal
和protected
都是等效的。那么internal
比它们更严格,而private
是最严格的访问。如果所有实例构造函数都是
私有
,则只有嵌套在相关类中的类才能从中继承。注意:如果没有为非静态类提供实例构造函数,则编译器将自行生成一个实例构造函数。这是一个带有零参数的构造函数。如果该类是抽象类,则自动生成的构造函数将受到
保护
。否则它是公开
。我能想到的唯一情况是,抽象类的实例构造函数是公共的还是受保护的,这会产生影响,那就是当您使用反射时。举个例子,
如果唯一的构造函数是受保护的,则表示将给出一个空数组;如果它是公共的,则将给出一个长度为 1 的数组。但当然,有一些重载指定了 BindingFlags,所以这不是问题,只是使用反射时需要记住的一点。
Inside an abstract class, for an instance constructor, modifiers
public
,protected internal
, andprotected
are all equivalent. Theninternal
is more strict than them, andprivate
is the most strict access.If all instance constructors are
private
, only classes nested inside the class in question can inherit from it.Note: If no instance constructors are given for a non-static class, then the compiler will generate one by itself. That's a constructor taking zero arguments. If the class is abstract, that auto-generated constructor is
protected
. Otherwise it ispublic
.The only situation I can think of where it makes a difference if an instance constructor of an abstract class is
public
orprotected
, is when you use reflection. As an example, sayingwill give an empty array if the sole constructor is
protected
, and a length-1 array if it'spublic
. But of course there are overloads that specifyBindingFlags
, so this is not a problem, just something to remember if one uses reflection.Dupe:还有另一个问题,就像这样: 抽象类构造函数访问修饰符
这个问题的答案最终归结为同一件事:声明它是
protected
还是public
并不重要。文献中似乎也有一些关于它的讨论(例如在 框架设计指南)。此博文中引用了这一点: 好的设计或坏的设计抽象类?
Dupe: there is another question on SO just like this: Abstract class constructor access modifier
The answers on that question come down to the same thing in the end: it does not really matter if you declare it
protected
orpublic
.Also there seems to be some discussion about it in literature (e.g. in Framework Design Guidelines). This is referenced in this blogpost: Good design or bad design of abstract class?
是的,抽象类上的
public
构造函数毫无意义,而且有点误导,因为它的行为就像受保护的一样,只有派生类可以调用它。除了有趣的边缘情况之外,
private
ctor 将没有什么意义。如果派生类需要,
protected
构造函数就有意义。Yes, a
public
ctor on an abstract class is meaningless and a bit misleading as it will behave as protected in that only derived classes may call it.A
private
ctor will have little meaning outside of interesting edge cases.A
protected
ctor would make sense if required by derived classes.