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

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

问题描述

我的问题涉及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 protected fields vs public getters( Java protected fields vs public getters ),但我仍然怀疑:

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;  

在抽象类中比以下情况更糟:

is worse in an abstract class than:

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.

谢谢!特定标题/页面#对书籍的引用对于..any basic Java programming text ...的引用更有帮助。

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

======== ================================== 10-13-2010

这是多少关于抽象类的问题,因为它是关于受保护的数据。我觉得令人失望的是,在OOP中数据隐藏是否是一件好事的回答似乎已经转移(答案:是)。这里有很多深度涉及抽象类的性质,以及它与常规非final类的区别,以及在抽象父类中修复数据项的名称和类型可能有哪些可能的优点供儿童班。我认为这里有可能将创新和更大的控制从抽象父类扩展到实现子类。我担心一般原则,例如数据隐藏的优点,可能成为教条,并抑制创新和新模式和想法的发展。

========================================== 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天全站免登陆