Java Getters and Setters

后端 未结 17 1563
终归单人心
终归单人心 2020-12-01 15:27

Is there a better standard way to create getters and setters in Java?

It is quite verbose to have to explicitly define getters and setters for each variable. Is ther

17条回答
  •  暖寄归人
    2020-12-01 16:17

    I've created some annotations that are not eclipse-specific.

    See http://code.google.com/p/javadude/wiki/Annotations

    For example:

    package sample;
    
    import com.javadude.annotation.Bean;
    import com.javadude.annotation.Property;
    import com.javadude.annotation.PropertyKind;
    
    @Bean(properties={
        @Property(name="name"),
        @Property(name="phone", bound=true),
        @Property(name="friend", type=Person.class, kind=PropertyKind.LIST)
    }) 
    public class Person extends PersonGen {
    }
    

    My annotations generate a superclass; I think Lombok modifies the actual class being compiled (which is officially not supported by Sun and may break - I could be wrong about how it works, but based on what I've seen they must be doing that)

    Enjoy! -- Scott

提交回复
热议问题