“公众”的相关性抽象类中的构造函数

发布于 2024-08-31 00:43:31 字数 334 浏览 6 评论 0原文

抽象类中的 public 构造函数是否有任何相关性? 我想不出任何可能的使用方法,在这种情况下,编译器不应该将其视为错误(C#,不确定其他语言是否允许)。

示例代码:

internal abstract class Vehicle
{
    public Vehicle()
    {            
    }
}

C# 编译器允许编译此代码,但我无法从外部调用此构造函数。它只能从派生类中调用。

那么它不应该只允许 protectedprivate 修饰符吗?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

红尘作伴 2024-09-07 00:43:31

没有理由为抽象类提供公共构造函数。我认为编译器不抱怨的原因很简单,他们只是没有花时间讨论这一点,因为它是公共的还是受保护的并不重要。

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.

抽个烟儿 2024-09-07 00:43:31

在抽象类中,对于实例构造函数,修饰符 publicprotectedinternalprotected 都是等效的。那么internal比它们更严格,而private是最严格的访问。

如果所有实例构造函数都是私有,则只有嵌套在相关类中的类才能从中继承。

注意:如果没有为非静态类提供实例构造函数,则编译器将自行生成一个实例构造函数。这是一个带有零参数的构造函数。如果该类是抽象类,则自动生成的构造函数将受到保护。否则它是公开

我能想到的唯一情况是,抽象类的实例构造函数是公共的还是受保护的,这会产生影响,那就是当您使用反射时。举个例子,

ConstructorInfo[] ctors = typeof(Vehicle).GetConstructors();

如果唯一的构造函数是受保护的,则表示将给出一个空数组;如果它是公共的,则将给出一个长度为 1 的数组。但当然,有一些重载指定了 BindingFlags,所以这不是问题,只是使用反射时需要记住的一点。

Inside an abstract class, for an instance constructor, modifiers public, protected internal, and protected are all equivalent. Then internal is more strict than them, and private 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 is public.

The only situation I can think of where it makes a difference if an instance constructor of an abstract class is public or protected, is when you use reflection. As an example, saying

ConstructorInfo[] ctors = typeof(Vehicle).GetConstructors();

will give an empty array if the sole constructor is protected, and a length-1 array if it's public. But of course there are overloads that specify BindingFlags, so this is not a problem, just something to remember if one uses reflection.

猫卆 2024-09-07 00:43:31

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 or public.

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?

黒涩兲箜 2024-09-07 00:43:31

是的,抽象类上的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文