对字段和方法使用私有而不是受保护的原因 [英] Reasons to use private instead of protected for fields and methods

查看:85
本文介绍了对字段和方法使用私有而不是受保护的原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常基本的面向对象问题,但是这个问题困扰了我一段时间.

This is a rather basic OO question, but one that's been bugging me for some time.

我倾向于避免在字段和方法中使用私有"可见性修饰符,而倾向于使用protected.

I tend to avoid using the 'private' visibility modifier for my fields and methods in favor of protected.

这是因为,通常来说,除了要为类的扩展设置特定的准则(即在框架中)时,我看不出在基础类和子类之间隐藏实现的任何用处.在大多数情况下,我认为试图限制我或其他用户扩展班级的做法是没有好处的.

This is because, generally, I don't see any use in hiding the implementation between base class and child class, except when I want to set specific guidelines for the extension of my classes (i.e. in frameworks). For the majority of cases I think trying to limit how my class will be extended either by me or by other users is not beneficial.

但是,对于大多数人而言,private修饰符通常是定义非公共字段/方法时的默认选择.

But, for the majority of people, the private modifier is usually the default choice when defining a non-public field/method.

那么,您可以列出private的用例吗?始终使用私人的主要原因是什么?还是您还认为它被过度使用了?

So, can you list use cases for private? Is there a major reason for always using private? Or do you also think it's overused?

推荐答案

人们已经达成共识,应该在OOP中更喜欢组合而不是继承.造成这种情况的原因有很多(如果您有兴趣,可以使用Google),但是主要的原因是:

There is some consensus that one should prefer composition over inheritance in OOP. There are several reasons for this (google if you're interested), but the main part is that:

  • 继承很少是最好的工具,而且不如其他解决方案灵活
  • 受保护的成员/字段构成了指向您的子类的接口
  • 接口(以及有关它们将来使用的假设)很难正确设置并正确记录

因此,如果您选择使您的类可继承,则应自觉地做到这一点,并牢记所有优点和缺点.

Therefore, if you choose to make your class inheritable, you should do so conciously and with all the pros and cons in mind.

因此,最好不要使该类可继承,而应使用其他方法确保它尽可能灵活(并且不再灵活).

Hence, it's better not to make the class inheritable and instead make sure it's as flexible as possible (and no more) by using other means.

这在大型框架中很明显,在大型框架中,类的使用超出了您的控制范围.对于您自己的小应用程序,您不会注意到太多的 ,但是如果您不注意的话,它(默认情况下是继承)会或早或晚地在您的后面咬住您.

This is mostly obvious in larger frameworks where your class's usage is beyond your control. For your own little app, you won't notice this as much, but it (inheritance-by-default) will bite you in the behind sooner or later if you're not careful.

替代

组成意味着您将通过显式(完全抽象的)界面(基于虚拟或基于模板)公开可定制性.

Composition means that you'd expose customizability through explicit (fully abstract) interfaces (virtual or template-based).

因此,您将拥有一个带有Motor接口对象的Vehicle类,而不是使用带有虚拟drive()函数的Vehicle基类(以及其他所有东西,例如价格的整数等),以及电机接口仅公开drive()函数.现在,您可以在任何地方(或多或少.:)添加和重复使用任何类型的电动机.

So, instead of having an Vehicle base class with a virtual drive() function (along with everything else, such as an integer for price, etc.), you'd have a Vehicle class taking a Motor interface object, and that Motor interface only exposes the drive() function. Now you can add and re-use any sort of motor anywhere (more or less. :).

这篇关于对字段和方法使用私有而不是受保护的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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