java POJO 类中“is”变量 getter/setter 的正确语法是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1118261/
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
What is the correct syntax for "is" variable getter/setters in a POJO class?
提问by Nik Reiman
If a class contains a variable named "blah", then the standard getter/setter syntax is obviously getBlah() and setBlah(). But if I have a POJO class with a variable named isBlah, would I use:
如果一个类包含一个名为“blah”的变量,那么标准的 getter/setter 语法显然是 getBlah() 和 setBlah()。但是如果我有一个名为 isBlah 的变量的 POJO 类,我会使用:
public type getIsBlah() {
return isBlah;
}
public setIsBlah(type isBlah) {
this.isBlah = isBlah;
}
Or would it be this?
或者会是这个?
public type isBlah() {
return isBlah;
}
public setBlah(type blah) {
this.isBlah = blah;
}
The first seems to conform more strictly to the POJO conventions, but the second type is what IntelliJ generates if I ask it to make a class' getter/setters (and hey, IntelliJ has never let me down yet :] ). So which is the preferred syntax?
第一种似乎更严格地符合 POJO 约定,但第二种类型是 IntelliJ 生成的,如果我要求它创建一个类的 getter/setter(嘿,IntelliJ 从来没有让我失望 :] )。那么哪个是首选语法?
回答by Jon Skeet
One reason for using properties is to decouple the API from the implementation. In other words, you shouldn't feel bound by what your private variable is called. That shouldn't inform the naming beyond trying to keep it readable to code maintainers.
使用属性的原因之一是将 API 与实现分离。换句话说,你不应该被你的私有变量的调用所束缚。除了试图保持代码维护人员的可读性之外,这不应告知命名。
I would say that if "type" is booleanin this case, then the second form is correct. If it's notboolean, you should use getXXX- but I probably wouldn't use getIsXXX. To me, "is" has a very strong correspondence with Boolean properties, and using it in other contexts would not only break the JavaBeans conventions (which could affect other tools) but would be misleading IMO.
我会说,如果boolean在这种情况下是“类型” ,那么第二种形式是正确的。如果不是boolean,您应该使用getXXX- 但我可能不会使用getIsXXX. 对我来说,“is”与布尔属性有很强的对应关系,在其他上下文中使用它不仅会破坏 JavaBeans 约定(这可能会影响其他工具),而且会误导 IMO。
回答by Joachim Sauer
Note that the name of the field is completely irrelevant to the JavaBean specification. Only the names of the getter/setter are relevant.
请注意,该字段的名称与JavaBean 规范完全无关。只有 getter/setter 的名称是相关的。
Normally the name of the getter is get<PropertyName>(). Only for booleanproperties is is<PropertyName>()allowed as an alternative.
通常,getter 的名称是get<PropertyName>(). 仅允许用于boolean属性is<PropertyName>()作为替代。
Note that in your example the Bean property name is "Blah" when you call the getter isBlah()and it's "IsBlah" when you call your getter getIsBlah().
请注意,在您的示例中,当您调用 getter 时,Bean 属性名称是“Blah”,而当您调用 getter 时isBlah()它是“IsBlah” getIsBlah()。
Personally I usually prefer isBlah().
我个人通常更喜欢isBlah().
回答by skaffman
There's one big problem with the "is" syntax if you use JSTL, which is that JSTL EL doesn't recognise them. It's pretty stupid, but the designers of the JSTL EL didn't bother to check their logic for javabeans compliance.
如果您使用 JSTL,“is”语法存在一个大问题,即 JSTL EL 无法识别它们。这很愚蠢,但 JSTL EL 的设计者并没有费心检查他们的逻辑是否符合 javabeans。
I often find myself writing getIsBlah() methods in my view-layer classes, which call isBlah(), just to give JSTL a hook. It's horrible.
我经常发现自己在我的视图层类中编写 getIsBlah() 方法,这些方法调用 isBlah(),只是为了给 JSTL 一个钩子。这太糟糕了。
回答by Nick Holt
回答by Greg Hewgill
I would also choose your second option. The first, with getIsBlah()seems wordy and redundant.
我也会选择你的第二个选项。第一个,withgetIsBlah()显得冗长而多余。
回答by aberrant80
Both the "get" and the "is" is fine actually, as they are technically still acceptable under the JavaBeans convention. I'd go for whichever sounds better or more natural, depending on what word your "Blah" actually is.
“get”和“is”实际上都很好,因为它们在 JavaBeans 约定下在技术上仍然可以接受。我会选择听起来更好或更自然的那个,这取决于你的“废话”实际上是什么词。
回答by thetoolman
JSTL only allows isMyBool if it is a boolean, not a Boolean or any other object, as per the bean spec. (primitive vs object).
根据 bean 规范,JSTL 仅允许 isMyBool 如果它是布尔值,而不是布尔值或任何其他对象。(原始与对象)。

