How do getters and setters work?

后端 未结 6 2117
北恋
北恋 2020-11-21 07:43

I\'m from the php world. Could you explain what getters and setters are and could give you some examples?

6条回答
  •  天命终不由人
    2020-11-21 07:47

    Tutorial is not really required for this. Read up on encapsulation

    private String myField; //"private" means access to this is restricted
    
    public String getMyField()
    {
         //include validation, logic, logging or whatever you like here
        return this.myField;
    }
    public void setMyField(String value)
    {
         //include more logic
         this.myField = value;
    }
    

提交回复
热议问题