抽象类中的受保护数据 [英] protected data in abstract class

查看:32
本文介绍了抽象类中的受保护数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题特别涉及 Java、抽象类和受保护数据的使用.有人告诉我,所有数据都应该是私有的,并且只能使用受保护的 getter/setter.

My question involves specifically Java, abstract classes, and the use of protected data. I am being told that all the data should be private, and protected getters/setters used only.

现在,我知道我们希望保护数据免受类的临时用户的直接操作,并且公共数据成员通常是一种有问题的做法.我看过Java 保护字段与公共 getter"(Java 保护字段与公共 getter ),但我仍然怀疑:

Now, I understand we want to shield data from direct manipulation by casual users of the class, and that public data members in general are a questionable practice. I have looked at "Java protected fields vs public getters" ( Java protected fields vs public getters ), but I still am dubious that:

protected int i;  

在抽象类中比:

private int i;  
protected int geti();  
protected void seti(int j); 

当抽象类正好为子类提供父类/公共设施时,我只是没有看到不利的一面,受保护的范围旨在提供对子类的访问,同时保护数据免受临时用户的侵害.我在上面提到的问题中注意到,大多数答案似乎解决了为什么数据一般应该是私有的而不是公开的问题.我试图将我的问题专门集中在供孩子使用的抽象父级中存在的数据上.迄今为止,我听到的唯一合理的评论是,使用父母保护的数据(例如,上面的 int i)会在子类中留下代码,这些代码引用了未在子类中声明的变量.这个论点不太引人注目(请参阅基类中的公共受保护数据成员? ) 您可能希望有一天更改访问权限,现在您必须尊重您的界面.这是一个抽象类,旨在 100% 的时间进行扩展.

I am just not seeing the down side when the abstract class is there precisely to provide parent/common facility to the children classes, and the protected scope is meant to provide access to children, while protecting the data from casual users. I note in the question referenced above, that most of the answers seem to address the issue of why data in general should be private rather than public. I am trying to focus my question specifically on data existing in an abstract parent intended for use by the children. The sole reasonable comment I have heard to date is that using the parents protected data (e.g., int i above) leaves you with code in the child class that references a variable not declared in the child class. Less compelling is the argument (see Common protected data member in base class? ) that you may want to change the access some day, and now you have to honor your interface. This is an abstract class, and is intended to be extended 100% of the time.

谢谢!对书籍的特定标题/页面# 引用比对..任何基本的 Java 编程文本..."的引用更有帮助

Thanks! Specific Title/page# references to books are far more helpful that references to "..any basic Java programming text..."

========================================== 10-13-2010
这是一个关于抽象类的问题,也是关于受保护数据的问题.我感到失望的是,焦点似乎已经转移到对数据隐藏在 OOP 中是否是一件好事的反应上(答案:是).这里有很多深度涉及抽象类的性质,以及它与常规非最终类的区别,以及修复抽象父类中数据项的名称和类型以供使用的可能优势儿童班.我认为这里有可能将创新和更大的控制从抽象父类扩展到实现子类.我担心一般原则,例如数据隐藏的优势,可能会成为教条,并抑制创新和新模式和想法的发展.

========================================== 10-13-2010
This was as much a question about abstract classes as it is about protected data. I find it disappointing that the focus seems to have shifted in the responses to whether data hiding is a good thing in OOP (answer: yes). There's a lot of depth here involving the nature of the abstract class, and how it differs from a regular non-final class, and what possible advantages there might be for fixing the names and types of data-items in the abstract parent for use by the child classes. I think there is the possibility here for innovation and greater control being extended down from the abstract parent to the implementing child classes. I am concerned that general principles, such as the advantages of data-hiding, can become dogma, and inhibit innovation and the development of new patterns and ideas.

感谢所有贡献者.

推荐答案

如果字段是私有的并且通过 getter 和 setter 访问,您将能够重新实现 getter 和 setter(例如,删除字段和更新/读取来自外部源的值),从而在不触及任何子类的情况下改变字段"的工作方式.

If the field is private and access is through getters and setters, you will be able to reimplement getters and setters (for instance, dropping the field and updating/reading the value from an external source), and thus change how the "field" works without touching any child classes.

这是否值得,这取决于您.

Whether this is worth it, that's up to you.

这篇关于抽象类中的受保护数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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