为什么要对抽象类的构造函数加以保护,不公开?函数、不公开、为什么要对、抽象类

2023-09-03 02:20:57 作者:北岛旅客

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.