简体   繁体   English

正确的JavaBean的getter和setter

[英]Correct getter and setter for a JavaBean

I have a class which is used as a DTO I am wondering what the correct naming for the getter and setter methods is. 我有一个用作DTO的类,我想知道getter和setter方法的正确命名是什么。

Current code: 当前代码:

public class NiceClass {
    private String PE_DATAB;

    public void setPE_DATAB(final String PE_DATAB) {
        this.PE_DATAB = PE_DATAB;
    }
}

But after reading the JavaBeansSpec my best guess is: 但是在阅读JavaBeansSpec之后,我的最佳猜测是:

public void setPe_DATAB(final String PE_DATAB) {
    this.PE_DATAB = PE_DATAB;
}

What is the correct naming for the setter method above for the variable PE_DATAB ? 上面对于变量PE_DATAB的setter方法的正确命名是什么?

I can not rename the variable! 我不能重命名变量!

You can check with your IDE quicker for this. 您可以更快地使用IDE进行检查。 However this is the getter and setter any framework will invoke: 但是,这是任何框架都将调用的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;
}

Please note that as you know, PE_DATAB is not a naming convention to follow. 请注意,您知道,PE_DATAB不是要遵循的命名约定。

public class NiceClass {
    private String PE_DATAB;

    public void setPE_DATAB(final String PE_DATAB) {
        this.PE_DATAB = PE_DATAB;
    }
}

is the correct code according to the JavaBeans specification. 是根据JavaBeans规范的正确代码。

You can check that it is correct with the following code: 您可以使用以下代码检查其是否正确:

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;
        }

    }

}

This is defined in section 8.3.1 of the JavaBean specification. 这在JavaBean规范的8.3.1节中定义。 Quoting: 报价:

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> . 如果我们发现一对匹配的get<PropertyName>set<PropertyName>方法采用并返回相同的类型,则我们将这些方法视为定义一个读写属性,其名称将为<propertyName>

Adding Setter and Getter Methods . 添加setter和getter方法 The getter and setter method should contains the name of the variable they are returning or setting. getter和setter方法应包含它们正在返回或设置的变量的名称。 You can use the above mentioned tutorial from Java Documentation. 您可以使用Java文档中的上述教程。 The standard for getter and setter are same irrespective of application. 不管应用程序如何,吸气剂和设置器的标准都是相同的。 Please read about Java Pojo . 请阅读有关Java Pojo的内容

If you are using any IDE for development, then your IDE can generate getter and setter without any efforts. 如果您使用任何IDE进行开发,则您的IDE可以毫不费力地生成getter和setter。

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

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