封装类方法/字段是否重要,如果它们仅被类本身访问?

huangapple go评论67阅读模式
英文:

Does encapsulation of class methods/fields matter if they are only accessed by the class itself?

问题

短文背景:我用Java制作了一个俄罗斯方块克隆游戏(所有的游戏数据和方法都封装在一个类中),我担心在这种情况下封装是否重要。

几乎所有的字段和方法都标记为"default",没有任何getter和setter,因为不希望用户访问它们。

方法直接操作变量,没有传递任何参数,当然,只有在它正在操作的内容是唯一的时候(不能有两个当前方块、两个下一个方块列表、两个保存的方块等等)。

  1. 如果用户永远不会获取类的原始成员,我真的需要getter和setter吗?方法直接获取和设置值。如果我有一个简单的int要获取或设置,我只是直接操作它。

  2. 没有返回值,如果每次都应该发生一件确切的事情。例如:如果在生成过程中发生碰撞,就立即触发gameOver(),而不是返回false,然后在函数外部执行。我选择没有返回值,因为在函数内部执行比在每个函数调用周围都加上执行相同操作的if语句要简单得多。

我需要修复其中的一些问题吗?如何修复,同时保持稳定性和可读性以及性能?

英文:

Short context: I made a tetris clone in java (all the game data and methods are a Class) and I am concerned about whether encapsulation matters in this case.

Almost all of the fields and methods are marked as "default", without any getters and setters because the user is not supposed to access them.

Methods manipulate variables directly without any arguments passed, of course only when the thing it is manipulating is unique (there can not be two current pieces, two next block lists, two held pieces and so on)

  1. Do I really need getters and setters if the user is never supposed to get raw members of the class? Methods just directly get and set the values. If I have a simple int that I want to get or set, I am just doing it directly.

  2. No return value, if one exact thing is supposed to happen every time. For example: if a collision happens during spawning, gameOver() is triggered immediately instead of returning false, then doing it outside of function. I chose to not have a return value because it is much simpler to do it inside function instead of surrounding each function call with an if statement doing the same thing.

Do I need to fix some of these things, and how should I, preserving stability in both readability and performance?

答案1

得分: 0

不会。

为什么呢?理解是,类的字段和方法共同朝着一个共同的目标而工作。因此,它们不是敌人,也不是粗心的行为者,你不必保护自己免受它们的影响。

仅仅是为了防止外部行为者干扰类不变量,你要尽量隐藏你的字段和方法,只暴露必要的部分。

如果你必须使用设置器和获取器来强制执行类内部的代码纪律,那么你要处理的问题比维护适当的封装(和其他面向对象原则)还要大得多。

英文:

It does not.

Why? The understanding is that a class’s fields and methods are working in conjunction towards a common goal. So they are not enemies or careless actors that you have to protect yourself against.

It is only to prevent external actors from mucking up class invariants that you hide your fields and methods as much as possible and expose only that which is necessary.

If you have to use setters and getters to enforce discipline in the code within a class, then you have much bigger problems to handle than maintaining proper encapsulation (and other OO principles.)

答案2

得分: 0

不,如果您不打算从类外部访问成员变量,则不需要使用getter和setter。

在您的情况下,因为成员变量仅从类内部访问,所以访问修饰符应更改为private

使用default关键字意味着您不想提供访问修饰符,该变量应对同一包中的任何其他类可用。

更多信息可以在这里找到。

英文:

No, you do not need getters and setters if you do not intend on anything accessing the member variables from outside of the class.

In your case because the member variables are only accessed from within the class itself then the access modifier(s) should be changed to private.

The use of the default keyword means that you do not want to provide an access modifier and that variable should be available to any other class in the same package.

More info can be found Here

huangapple
  • 本文由 发表于 2020年10月23日 09:25:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/64492642.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定