更正 JavaBean 的 getter 和 setter
Correct getter and setter for a JavaBean
我有一个用作 DTO 的 class 我想知道 getter 和 setter 方法的正确命名是什么。
当前代码:
public class NiceClass {
private String PE_DATAB;
public void setPE_DATAB(final String PE_DATAB) {
this.PE_DATAB = PE_DATAB;
}
}
但在阅读 JavaBeansSpec 之后,我的最佳猜测是:
public void setPe_DATAB(final String PE_DATAB) {
this.PE_DATAB = PE_DATAB;
}
上述 setter 方法对变量 PE_DATAB
的正确命名是什么?
我无法重命名变量!
您可以更快地与您的 IDE 核实一下。然而,这是 getter 和 setter 任何框架都会调用的:
private String PE_DATAB;
public String getPE_DATAB() {
return PE_DATAB;
}
public void setPE_DATAB(String PE_DATAB) {
this.PE_DATAB = PE_DATAB;
}
请注意,如您所知,PE_DATAB 不是要遵循的命名约定。
public class NiceClass {
private String PE_DATAB;
public void setPE_DATAB(final String PE_DATAB) {
this.PE_DATAB = PE_DATAB;
}
}
是符合 JavaBeans 规范的正确代码。
您可以通过以下代码检查是否正确:
public class Main {
public static void main(String[] args) throws Exception {
BeanInfo info = Introspector.getBeanInfo(NiceClass.class);
System.out.println("Setter: " + info.getPropertyDescriptors()[0].getWriteMethod());
// prints "Setter: public void Main$NiceClass.setPE_DATAB(java.lang.String)"
System.out.println("Name of variable: " + info.getPropertyDescriptors()[0].getName());
// prints "Name of variable: PE_DATAB"
}
public class NiceClass {
private String PE_DATAB;
public void setPE_DATAB(String PE_DATAB) {
this.PE_DATAB = PE_DATAB;
}
}
}
这是在 JavaBean 规范的第 8.3.1 节中定义的。引用:
By default, we use design patterns to locate properties by looking for methods of the form:
public <PropertyType> get<PropertyName>();
public void set<PropertyName>(<PropertyType> a);
If we discover a matching pair of get<PropertyName>
and set<PropertyName>
methods that take and return the same type, then we regard these methods as defining a read-write property whose name will be <propertyName>
.
Adding Setter and Getter Methods. The getter and setter method should contains the name of the variable they are returning or setting. You can use the above mentioned tutorial from Java Documentation. The standard for getter and setter are same irrespective of application. Please read about Java Pojo.
如果您正在使用任何 IDE 进行开发,那么您的 IDE 可以毫不费力地生成 getter 和 setter。
我有一个用作 DTO 的 class 我想知道 getter 和 setter 方法的正确命名是什么。
当前代码:
public class NiceClass {
private String PE_DATAB;
public void setPE_DATAB(final String PE_DATAB) {
this.PE_DATAB = PE_DATAB;
}
}
但在阅读 JavaBeansSpec 之后,我的最佳猜测是:
public void setPe_DATAB(final String PE_DATAB) {
this.PE_DATAB = PE_DATAB;
}
上述 setter 方法对变量 PE_DATAB
的正确命名是什么?
我无法重命名变量!
您可以更快地与您的 IDE 核实一下。然而,这是 getter 和 setter 任何框架都会调用的:
private String PE_DATAB;
public String getPE_DATAB() {
return PE_DATAB;
}
public void setPE_DATAB(String PE_DATAB) {
this.PE_DATAB = PE_DATAB;
}
请注意,如您所知,PE_DATAB 不是要遵循的命名约定。
public class NiceClass {
private String PE_DATAB;
public void setPE_DATAB(final String PE_DATAB) {
this.PE_DATAB = PE_DATAB;
}
}
是符合 JavaBeans 规范的正确代码。
您可以通过以下代码检查是否正确:
public class Main {
public static void main(String[] args) throws Exception {
BeanInfo info = Introspector.getBeanInfo(NiceClass.class);
System.out.println("Setter: " + info.getPropertyDescriptors()[0].getWriteMethod());
// prints "Setter: public void Main$NiceClass.setPE_DATAB(java.lang.String)"
System.out.println("Name of variable: " + info.getPropertyDescriptors()[0].getName());
// prints "Name of variable: PE_DATAB"
}
public class NiceClass {
private String PE_DATAB;
public void setPE_DATAB(String PE_DATAB) {
this.PE_DATAB = PE_DATAB;
}
}
}
这是在 JavaBean 规范的第 8.3.1 节中定义的。引用:
By default, we use design patterns to locate properties by looking for methods of the form:
public <PropertyType> get<PropertyName>();
public void set<PropertyName>(<PropertyType> a);
If we discover a matching pair of
get<PropertyName>
andset<PropertyName>
methods that take and return the same type, then we regard these methods as defining a read-write property whose name will be<propertyName>
.
Adding Setter and Getter Methods. The getter and setter method should contains the name of the variable they are returning or setting. You can use the above mentioned tutorial from Java Documentation. The standard for getter and setter are same irrespective of application. Please read about Java Pojo.
如果您正在使用任何 IDE 进行开发,那么您的 IDE 可以毫不费力地生成 getter 和 setter。