Java Getter 和 Setter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1907312/
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
Java Getters and Setters
提问by Verhogen
Is there a better standard way to create getters and setters in Java?
有没有更好的标准方法来在 Java 中创建 getter 和 setter?
It is quite verbose to have to explicitly define getters and setters for each variable. Is there a better standard annotations approach?
必须为每个变量显式定义 getter 和 setter 是非常冗长的。有没有更好的标准注释方法?
Does Spring have something like this?
Spring有这样的东西吗?
Even C# has properties.
甚至 C# 也有属性。
回答by Ascalonian
Eclipse has a context menu option that will auto-generate these for you, as I am sure many other IDE's do.
Eclipse 有一个上下文菜单选项,可以为您自动生成这些选项,我相信许多其他 IDE 都会这样做。
回答by Joel
Most IDEs provide a shortcut for generating the code (e.g. Eclipse: Right click -> Source -> Generate Getters & Setters), although I realise this is probably not the answer you are looking for.
大多数 IDE 提供了生成代码的快捷方式(例如Eclipse:右键单击 -> 源 -> 生成 Getters & Setters),尽管我意识到这可能不是您正在寻找的答案。
Some IOC frameworks allow you to annotate properties so that they can be used in the context of the framework e.g. Tapestry IOC, and the latest Spring, I think (but this use is limted to use by the framework)
一些 IOC 框架允许您注释属性,以便它们可以在框架的上下文中使用,例如 Tapestry IOC 和最新的 Spring,我认为(但这种用法仅限于框架使用)
回答by TheBigS
Yeah, you are kind of out of luck. Groovy does generate them for you, but no dice in standard java. If you use Eclipse you can generate them pretty easily, as well as generate hashCode() and equals() functions.
是的,你有点不走运。Groovy 确实为您生成它们,但在标准 java 中没有骰子。如果您使用 Eclipse,您可以非常轻松地生成它们,以及生成 hashCode() 和 equals() 函数。
回答by Carl Smotricz
I'm not sure if you'd consider it 'standard', but Project Lombokaddresses this problem. They use annotations to replace much of the verbosity of Java.
我不确定您是否认为它是“标准的”,但Project Lombok解决了这个问题。他们使用注释来代替 Java 的大部分冗长。
Some people are looking at alternative Java sibling languages, such as Groovy or Scala. I'm afraid it will take some years -if at all- before the JSR figures out a "standardized" way to "fix" this in Java proper.
有些人正在寻找替代的 Java 兄弟语言,例如 Groovy 或 Scala。恐怕要花几年的时间——如果有的话——JSR 才能找到一种“标准化”的方法来在 Java 中适当地“修复”这个问题。
回答by Jerome Cance
Here is an interesting articles about the subject: http://cafe.elharo.com/java/why-java-doesnt-need-properties-it-already-has-them/
这是关于该主题的有趣文章:http: //cafe.elharo.com/java/why-java-doesnt-need-properties-it-already-has-them/
I think properties are a shortcut but it's more a little feature than a real important feature
我认为属性是一个捷径,但它更多的是一个小功能而不是一个真正重要的功能
回答by monksy
With Netbeans, just start typing get or set where the getter/setter is to be replaced and call up auto complete (Ctrl+Space), it'll give you the option to generate the getter or setter. It'll also give you an option to generate a constructor as well.
使用 Netbeans,只需在要替换 getter/setter 的位置开始键入 get 或 set 并调用自动完成 (Ctrl+Space),它就会为您提供生成 getter 或 setter 的选项。它还会为您提供生成构造函数的选项。
回答by kdgregory
There is no better way that's part of the language -- nothing like a "property" keyword.
没有更好的方法是语言的一部分 - 没有什么像“属性”关键字那样。
One alternative, as other people have mentioned, is to use your IDE to generate them. Another, if you have a lotof objects that need this, is to write your own code generation tool that takes a base class and produces a wrapper with getters and setters.
正如其他人所提到的,另一种选择是使用您的 IDE 来生成它们。另一个,如果你有很多需要这个的对象,是编写你自己的代码生成工具,它接受一个基类并生成一个带有 getter 和 setter 的包装器。
You could also simply expose the variables as public members. However, down the road this will probably come back to hurt you, when you decide to add validation logic.
您也可以简单地将变量公开为公共成员。然而,当您决定添加验证逻辑时,这可能会再次伤害您。
However, one final thought: unless your classes are used simply to transfer data, they probably shouldn't expose their internal state. IMO, "behavior" classes with getters and setters are a code smell.
但是,最后一个想法:除非您的类仅用于传输数据,否则它们可能不应该公开其内部状态。IMO,带有 getter 和 setter 的“行为”类是一种代码气味。
回答by Martin Wickman
As a possible alternative, have you tried Scala? It compiles to Java bytecode, and has lots of interesting shortcuts which can make your life as a Java programmer easier.
作为可能的替代方案,您是否尝试过Scala?它编译为 Java 字节码,并有许多有趣的快捷方式,可以让您作为 Java 程序员的生活更轻松。
Properties for instance:
属性例如:
case class Person(var name:String,
var age:Int);
val p = Person("John", 4)
p.name
p.name = "Charlie"
p.name
And the output:
和输出:
defined class Person
p: Person = Person(John,4)
res7: String = John
res8: String = Charlie
回答by user225486
That's the work of the IDE to generate repetitive verbose code as getters/setters
这是 IDE 生成重复冗长代码作为 getter/setter 的工作
回答by Ravi Wallau
Use your IDE to generate it for you and try to minimize the amount of getters/ setters that you have - you will likely enjoy the added benefit of immutability.
使用您的 IDE 为您生成它并尽量减少您拥有的 getter/setter 的数量 - 您可能会享受不变性的额外好处。
I like the C# syntax for properties, I think it's pretty and clean, and pretty clean.
我喜欢属性的 C# 语法,我认为它非常干净,而且非常干净。