简体   繁体   English

Java getters和setter只是方法吗?

[英]Are Java getters and setters simply methods?

Is the follwing true: Java getters and setters are just methods. 以下是真实的:Java getter和setter只是方法。

I know they are implemented to manipulate or get the value of private ... attributes/fields. 我知道它们被实现为操纵或获取私有...属性/字段的值。 But is it okay to call them methods? 但是可以称他们为方法吗?

是的,没关系,他们只是方法!

Technically, from the language and VM point of view, yes, they are just methods. 从技术上讲,从语言和VM的角度来看,是的,它们只是方法。

Some libraries and frameworks, however recognize their special nature. 然而,一些图书馆和框架认识到它们的特殊性。 For example, JPA may be used to map “properties” by annotating either fields or methods. 例如,JPA可用于通过注释字段或方法来映射“属性”。 Beans Binding library uses them to access properties, so if you have a getText() / setText() pair on some object, then you can bind the “text” property to some other property of some other object (you'll still have to do the addPropertyChangeListener magic, though). Beans Binding库使用它们来访问属性,所以如果你在某个对象上有一个getText() / setText()对,那么你可以将“text”属性绑定到某个其他对象的某个其他属性(你仍然需要尽管如此,请执行addPropertyChangeListener魔术。 But this is just a “convention over configuration” phenomena combined with the power of Reflection. 但这只是一种“配置约定”现象与反射的力量相结合。

As per JLS , 根据JLS

A method declares executable code that can be invoked, passing a fixed number of values as arguments. 方法声明可以调用的可执行代码,将固定数量的值作为参数传递。

And this criterias satisfies to getters and setters as well; 而且这个标准也适用于吸气剂和制定者; so we can say the are "methods" in java language. 所以我们可以说java语言中的“方法”。

Luckily I was able to pull Craig Larman's "Applying UML and Patterns" book section in google. 幸运的是,我能够在谷歌中推出Craig Larman的“应用UML和模式” 一书 As quoted 如引述

Accessing methods retrieve(accessor method) or set(mutator method)
attributes. In some languages such as Java it is a common idiom to 
have an accessor and mutator for each attribute, and to declare all
attributes private(to enforce data encapsulation). The methods are 
excluded in the class diagram because of the high noise-to-value ratio
they generate."

Java Getters and Setters are accessor methods. Java Getters和Setter是访问器方法。 So, yes, they are methods. 所以,是的,它们是方法。

Many programmers (Java or otherwise) may feel annoyed at constantly writing getXXX() and setXXX(type t) for all of their private fields, especially if they are basically just one line methods. 许多程序员(Java或其他程序员)可能会为所有私有字段不断编写getXXX()setXXX(type t)而烦恼,特别是如果它们基本上只是一行方法。 For this case, there are some annotation libraries (like Lombak ) that generate these through the power of metaprogramming and Java annotations and reflection. 对于这种情况,有一些注释库(如Lombak )通过元编程和Java注释和反射的功能生成这些库。

However, there are many reasons to explicitly write getters/setters. 但是,明确编写getter / setter的原因有很多。 A good list can be found here . 一个良好的名单,可以发现在这里 But I really like this answer , too. 但我也非常喜欢这个答案

Yes, essentially they are methods. 是的,基本上他们是方法。 The standard definition for a Java method is as follows; Java方法的标准定义如下:

A method is a set of code which is referred to by name and can be called (invoked) at any point in a program simply by utilizing the method's name. 方法是一组代码,由名称引用,只需使用方法名称即可在程序中的任何位置调用(调用)。

- https://mathbits.com/MathBits/Java/Methods/Lesson1.htm - https://mathbits.com/MathBits/Java/Methods/Lesson1.htm

So you can consider methods as small programs within a class itself that allows us to fulfill specific tasks, which is also exactly what getters and setters do. 因此,您可以将方法视为类本身中的小程序,这些方法允许我们完成特定任务,这也正是getter和setter所做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM