为什么抽象类的构造函数应该受到保护而不是公开?

时间:2022-09-25 12:24:37

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

ReSharper建议将抽象类中的公共构造函数的可访问性更改为受保护,但它没有说明这背后的基本原理。

Can you shed some light?

你能摆脱一些光吗?

3 个解决方案

#1


65  

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

仅仅因为公开在抽象类中没有任何意义。根据定义,抽象类不能直接实例化。它只能由派生类型的实例实例化。因此,唯一应该访问构造函数的类型是它的派生类型,因此受保护比公共更有意义。它更准确地描述了可访问性。

#2


13  

It technically makes no difference whatsoever if you make the constructor public instead of protected on an abstract class. The accessibility/visibility of the constructor is still exactly the same: the same class or derived classes. The two keywords have indistinguishable effects for all intents and purposes.

如果你将构造函数设置为public而不是在抽象类上保护,那么技术上没有任何区别。构造函数的可访问性/可见性仍然完全相同:相同的类或派生类。这两个关键字对于所有意图和目的都具有无法区分的效果。

So, this choice is only a matter of style: type protected to satisfy the Object Oriented savvy people.

所以,这个选择只是风格问题:类型受保护以满足面向对象的精明人。


Reflection will by default only include the constructor when it is public, but you cannot call that constructor anyway.

默认情况下,反射只包含构造函数,当它是公共的时,但无论如何都无法调用该构造函数。

IntelliSense will show the public constructor when typing new, but you cannot call that constructor anyway.

IntelliSense将在键入new时显示公共构造函数,但无论如何都无法调用该构造函数。

The assembly's metadata will reflect the fact that the constructor is public or protected.

程序集的元数据将反映构造函数是公共的还是受保护的。

#3


1  

It is good OO practice.

这是一个很好的OO练习。

public abstract class ExampleAbstractClass
{
    protected ExampleAbstractClass()
    {
      // :::
    }
}

You only want the inheriting child classes to have access to the constructor. The only way to do that is by making the constructor protected.
Keep in mind, when you add parameters to these constructors, it is an entirely different discussion.

您只希望继承子类可以访问构造函数。唯一的方法是使构造函数受到保护。请记住,当您向这些构造函数添加参数时,这是一个完全不同的讨论。

#1


65  

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

仅仅因为公开在抽象类中没有任何意义。根据定义,抽象类不能直接实例化。它只能由派生类型的实例实例化。因此,唯一应该访问构造函数的类型是它的派生类型,因此受保护比公共更有意义。它更准确地描述了可访问性。

#2


13  

It technically makes no difference whatsoever if you make the constructor public instead of protected on an abstract class. The accessibility/visibility of the constructor is still exactly the same: the same class or derived classes. The two keywords have indistinguishable effects for all intents and purposes.

如果你将构造函数设置为public而不是在抽象类上保护,那么技术上没有任何区别。构造函数的可访问性/可见性仍然完全相同:相同的类或派生类。这两个关键字对于所有意图和目的都具有无法区分的效果。

So, this choice is only a matter of style: type protected to satisfy the Object Oriented savvy people.

所以,这个选择只是风格问题:类型受保护以满足面向对象的精明人。


Reflection will by default only include the constructor when it is public, but you cannot call that constructor anyway.

默认情况下,反射只包含构造函数,当它是公共的时,但无论如何都无法调用该构造函数。

IntelliSense will show the public constructor when typing new, but you cannot call that constructor anyway.

IntelliSense将在键入new时显示公共构造函数,但无论如何都无法调用该构造函数。

The assembly's metadata will reflect the fact that the constructor is public or protected.

程序集的元数据将反映构造函数是公共的还是受保护的。

#3


1  

It is good OO practice.

这是一个很好的OO练习。

public abstract class ExampleAbstractClass
{
    protected ExampleAbstractClass()
    {
      // :::
    }
}

You only want the inheriting child classes to have access to the constructor. The only way to do that is by making the constructor protected.
Keep in mind, when you add parameters to these constructors, it is an entirely different discussion.

您只希望继承子类可以访问构造函数。唯一的方法是使构造函数受到保护。请记住,当您向这些构造函数添加参数时,这是一个完全不同的讨论。