为什么要对抽象类的构造函数加以保护,不公开? [英] Why should constructors on abstract classes be protected, not public?

查看:243
本文介绍了为什么要对抽象类的构造函数加以保护,不公开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ReSharper的建议改变的可访问一个公开构造在摘要保护,但它没有说明理由背后。

ReSharper suggests changing the accessibility of a public constructor in an abstract class to protected, but it does not state the rationale behind this.

您可以提供一些线索?

推荐答案

很简单,因为公职使得在一个抽象类,没有任何意义。一个抽象类定义不能直接创建。它只能通过创建派生类型的实例被创建。因此,应该有机会获得一个构造函数的唯一类型派生类型,因此保护更有道理公众。它更准确地描述了易用性。

Simply because being public makes no sense on an abstract class. An abstract class by definition cannot be created directly. It can only be created by creating an instance of a derived type. Therefore the only types that should have access to a constructor are derived types and hence protected makes much more sense that public. It more accurately describes the accessibility.

这篇关于为什么要对抽象类的构造函数加以保护,不公开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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