Fields vs. Properties

About Monkey 2 Forums Monkey 2 Programming Help Fields vs. Properties

This topic contains 1 reply, has 2 voices, and was last updated by  nobuyuki 2 years, 10 months ago.

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

    jondecker76
    Participant

    I can’t find any good documentation on properties.  What are their advantage over fields?  Is it just a matter of convenience so that you don’t have to write separate functions to handle getting/setting fields?

    #1621

    nobuyuki
    Participant

    Properties are a good way to expose class fields to other classes in an “encapsulated” manner.  In practical usage (outside of theory or library design), they’re a useful way to sneak in some maintenance code when performing a get/set operation on a field variable.  They’re also useful for creating fields that can’t be altered by certain classes.

    For example, you can set your underlying field variable as Protected or Private, then only provide a Property getter to access it.  If the field’s set to private, then only that class can change it directly — if it’s set to protected, then only that class and classes that inherit it can alter the field.  That’s good for keeping people away from messing with variables where they’re not supposed to…

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

You must be logged in to reply to this topic.