How do getters and setters work?

后端 未结 6 2053
北恋
北恋 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条回答
  •  萌比男神i
    2020-11-21 08:09

    You may also want to read "Why getter and setter methods are evil":

    Though getter/setter methods are commonplace in Java, they are not particularly object oriented (OO). In fact, they can damage your code's maintainability. Moreover, the presence of numerous getter and setter methods is a red flag that the program isn't necessarily well designed from an OO perspective.

    This article explains why you shouldn't use getters and setters (and when you can use them) and suggests a design methodology that will help you break out of the getter/setter mentality.

提交回复
热议问题