英文:
OOP encapsulation based on functionality of a program?
问题
我正在学习Java和面向对象编程的基础知识。假设我正在创建一个Person类。一个人当然有名字和姓氏,这就是为什么我在Person类中声明了两个私有变量,即firstname和lastname。它们在构造函数中获得初始值。现在,您如何决定它们是否都需要公共的getter和setter,或者只需要一个getter而不需要setter呢?您是否根据您的应用程序应具备的功能来做出这个决定?
例如,如果您正在为当地的运动俱乐部构建一个应用程序,用于跟踪他们的会员(每个会员都是一个person对象)。在应用程序中,有一个按钮用于创建新会员。如果您点击此按钮,您需要填写名字和姓氏,然后点击“创建并添加到俱乐部”按钮。在幕后,会创建一个person对象。该应用程序可以显示所有会员的列表,并且可以删除会员,这就是它能做的全部。
现在没有类似于更改会员名字的功能。这是否可能是我不应该为firstname和lastname设置setter的原因?因此,如果我在会员的名字中输入了一个拼写错误,我首先必须删除他,然后再次创建一个没有拼写错误的对象。如果应用程序有一个“更改名称”的按钮,我应该需要一个setter或类似于changeName的方法,因为我想要更改已经存在的对象。
这是否是正确的思路,或者功能与封装无关?
附:我知道这是一个简单的例子,只是为了基于我的问题。
谢谢
英文:
I'm learning Java and the basics of OOP. Imagine I'm making a Person class. A person has of course a firstname and a lastname, that's why I declare two private variables in the Person class, being firstname and lastname. Both getting their initial value in the constructor. Now, how do you decide wether they both need a public getter and a setter, or only a getter and not a setter? Do you take this decission based on the kind of functionalities your application should have?
For example if you are building an application for the local sportsclub to keep track of their members (each member is a person object). Within the application there is a button to create a new member. if you push this button you have to fill in a firstname and a lastname and push the button 'create and add to club'. Behind the scenes there is a person object created. The application can show a list of all members and delete a member and thats all it can do.
Now there is no functionality like change firstname or lastname of a member. Could this be a reason why I should not have a setter for firstname and lastname? So if I created a member maked a typo in his firstname, I first have to delete him and then create him again without the typo. If the application had a button 'change name' I should need a setter or a method like changeName or something because I want to alter an already existing object.
Is this the correct mindset or has functionality nothing to do with encapsulation?
Ps. I know it's simple example but it's just to base my question on.
Thanks
答案1
得分: 1
根据属性的特性,您会做出这种类型的决策。它们是公共的(被对象/类外部实体使用?)还是私有的(仅供对象/类内部使用?)?在构建后它们应该是只读的吗?当它们在“对外公开”时是只读的,您就不需要公共的设置器。如果在内部仍然希望更改它们,您可能可以直接访问它们,而不需要通过设置器进行访问。
此外,能够在不必销毁对象并重新创建对象的情况下修复错误的属性也是有道理的。
英文:
You make that kind of decisions based on the character of attributes. Are they public (used by external to the object/class entities?). Are they private (for object/class internal use only?). Should they be read-only after construction? When they are read-only "to the public", you don't need a public setter. If internally you still want to change them, you probably can access them directly without going through a setter.
It also makes sense to be able to fix an erroneous attribute without having to destroy the object and recreate it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论