Getters/setters in Java

后端 未结 7 1864
轻奢々
轻奢々 2021-02-18 13:18

I\'m new to Java, but have some OOP experience with ActionScript 3, so I\'m trying to migrate relying on stuff I know.

In ActionScript 3 you can create getters and sette

7条回答
  •  南旧
    南旧 (楼主)
    2021-02-18 14:08

    I would consider not having the getter or setter as they don't do anything in your case except make the code more complicated. Here is an example without getters or setters.

    class Dummy {
      public String name;
      public Dummy(String name) { this.name = name; }
    }
    
    Dummy dummy = new Dummy("fred");
    System.out.println(dummy.name);//getter, prints: fred
    dummy.name = "lolo";//setter
    System.out.println(dummy.name);//getter, prints: lolo
    

    IMHO Don't make things more complicated than you need to. It so often the case that adding complexity will suffer from You-Aint-Gonna-Need-It

提交回复
热议问题