在抽象类“公共”构造关联 [英] Relevance of 'public' constructor in abstract class

查看:192
本文介绍了在抽象类“公共”构造关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有公共的构造函数,抽象类中的任何关联? 我想不出来使用任何可能的方式,在这种情况下,应该不是被视为错误由编译器(C#,不知道其他语言允许的)。

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).

样品code:

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

C#编译器允许,code编译,但没有办法,我可以调用这个构造器从外面的世界。它可以被称为仅从派生类。 因此,它不应该允许'保护',只有'私人'修饰符。 请发表评论。

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. Please comment.

推荐答案

没有理由为一个公共构造一个抽象类。我认为,编译器不会抱怨的原因很简单,他们只是没有花时间覆盖,由于它真的并不重要,如果它是公共或受保护的关系。

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.

这篇关于在抽象类“公共”构造关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆