Get Set?

This topic contains 1 reply, has 2 voices, and was last updated by  Ethernaut 1 week, 4 days ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #16089

    Amon

    Participant

    How do these work?

    Can anyone offer me some help?

    I see a lot of monkey code that has get set in it and I’m wondering what it does and why?

    #16090

    Ethernaut

    Participant

    You can use Properties with Getter and Setter when:

    • You want an attribute to be “read only” (just the getter, not the setter). The property can do as little as returning a private field, but whoever is accessing the class won’t have access to write anything in the field (i.e. won’t break anything in your class).
    • You want something else to happen when you get or set a value. For instance, you can have a “Color” property in an entity, but when you set it it also sets the Color property of all the entity’s children, etc. A simple field can’t do that.

    I’m sure there’s other cases as well, have to go now!
    Cheers.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.