Java Getters and Setters

后端 未结 17 1538
终归单人心
终归单人心 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:02

    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.

    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
    

提交回复
热议问题