Getters/setters in Java

后端 未结 7 1844
轻奢々
轻奢々 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:06

    Your Java code is fine, except that you would, want to make _name private.

    There are no get and set keywords in Java as in your AS3 example. Sorry, it doesn't get better than what you're doing already.

    Corrected code:

    class Dummy {
      private String _name;
    
      public void Dummy() {}
    
      public void Dummy(String name) {
        setName(name);
      }
    
      public String getName() {
        return _name;
      }
    
      public void setName(String value) {
        _name = value;
      }
    }
    

提交回复
热议问题