Java 访问变量的 getter 方法调用比类中的直接变量访问更好

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23695683/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 00:45:46  来源:igfitidea点击:

is getter method call to access variable better than direct variable access within a class

javaclass

提问by Bwire

I am just curious to know if it advisable within a class instance to access the class variables using getter methods and if there are any tangible performance differences with direct access. Especially in situations where many objects are expected to be generated in the jvm.

我只是想知道是否建议在类实例中使用 getter 方法访问类变量,以及直接访问是否存在任何明显的性能差异。特别是在 jvm 中预计会生成许多对象的情况下。

采纳答案by Thomas Uhrig

In Java it is a convention to access all fields via getter/setters from outside the class. From inside the class, you usually access fields directly. However, you can also access them via getter/setter is you want to.

在 Java 中,通过 getter/setter 从类外部访问所有字段是一种约定。从类内部,您通常直接访问字段。但是,您也可以根据需要通过 getter/setter 访问它们。

It is important to know that this is just a convention. Many other programming languages don't have such strict rules or other concepts. So you are not forced to do that. But it is a good practice.

重要的是要知道这只是一个约定。许多其他编程语言没有如此严格的规则或其他概念。所以你不会被迫这样做。但这是一个很好的做法。

And: Don't mind performance! Using getters/setters will not affect the performance of your app. The JVM/Java is designed to work exactly like this. Every JVM will optimize your code and handle getters/setters in a very effective way. Try to write clear and good readable code.

并且:不要介意性能!使用 getter/setter 不会影响应用程序的性能。JVM/Java 被设计成完全像这样工作。每个 JVM 都会优化您的代码并以非常有效的方式处理 getter/setter。尝试编写清晰易读的代码。

回答by MCHAppy

I will answer your question in three parts :

我分三部分回答你的问题:

  1. Direct access (public members) is the worst solution :

    A public member can be accessed from outside the class, which for practical considerations means "potentially anywhere". If something goes wrong with a public field, the culprit can be anywhere, and so in order to track down the bug, you may have to look at quite a lot of code.

  2. Encapsulation ( private members ) :

    A private member, by contrast, can only be accessed from inside the same class, so if something goes wrong with that, there is usually only one source file to look at. If you have a million lines of code in your project, but your classes are kept small, this can reduce your bug tracking effort by a factor of 1000.

  3. Getters and Setters are highly Overused :

    Creating private fields and then using the IDE to automatically generate getters and setters for all these fields is almost as bad as using public fields.

    One reason for the overuse is that in an IDE it's just now a matter of few clicks to create these accessors. The completely meaningless getter/setter code is at times longer than the real logic in a class and you will read these functions many times even if you don't want to.

  1. 直接访问(公共成员)是最糟糕的解决方案:

    可以从类外部访问公共成员,出于实际考虑,这意味着“可能在任何地方”。如果公共字段出现问题,罪魁祸首可能在任何地方,因此为了追踪错误,您可能需要查看大量代码。

  2. 封装(私有成员):

    相比之下,私有成员只能从同一个类内部访问,因此如果出现问题,通常只有一个源文件可以查看。如果您的项目中有 100 万行代码,但您的类保持较小,这可以将您的错误跟踪工作减少 1000 倍。

  3. Getter 和 Setter 被过度使用:

    创建私有字段然后使用 IDE 为所有这些字段自动生成 getter 和 setter 几乎与使用公共字段一样糟糕。

    过度使用的一个原因是,在 IDE 中,现在只需单击几下即可创建这些访问器。完全无意义的 getter/setter 代码有时比类中的真实逻辑要长,即使您不想,您也会多次阅读这些函数。

CONCLUSION:

结论:

Use of accessors (getters and setters ) to restrict direct access to field variable is preferred over the use of public fields, however, making getters and setter for each and every field is overkill. It also depends on the situation though, sometimes you just want a dumb data object. Accessors should be added for field where they're really required. A class should expose larger behavior which happens to use its state, rather than a repository of state to be manipulated by other classes.

使用访问器(getter 和 setter)来限制对字段变量的直接访问优于使用公共字段,但是,为每个字段制作 getter 和 setter 是多余的。不过,这也取决于情况,有时您只需要一个愚蠢的数据对象。应该为真正需要的字段添加访问器。一个类应该暴露更大的行为,这些行为碰巧使用了它的状态,而不是一个由其他类操纵的状态存储库。

回答by Ganesa Vijayakumar

I believe getter method call is better than direct call because it will help to find all the references from both inner class and outside of the classes.

我相信 getter 方法调用比直接调用更好,因为它有助于找到来自内部类和类外部的所有引用。