what is a member vs. a property

后端 未结 9 2040
灰色年华
灰色年华 2020-12-15 03:39

A friend who is new to OO programming asked me the difference between a Member and Property, and I was ashamed to admit that I couldn\'t give him a good answer. Since proper

相关标签:
9条回答
  • 2020-12-15 04:24

    Properties is one kind of members.

    In C#, for example, a class can have the following members:

    • Constructors
    • Destructors
    • Constants
    • Fields
    • Methods
    • Properties
    • Indexers
    • Operators
    • Events
    • Delegates
    • Classes
    • Interfaces
    • Structs

    MSDN: C#: class

    0 讨论(0)
  • Members are just objects or primitive types belonging to a class.

    Properties give you more power than members. It's like a simplified way to create getters and setters letting you make, for instance, public getters and private setters; and put whatever logic you want in the way it will be read or written to. They can be used as a way to expose members, being possible to change the reading and writing policy later more easily.

    This applies to C#. Not sure if this is true for the other languages though.

    0 讨论(0)
  • 2020-12-15 04:28

    Both properties and methods are members of an object. A property describes some aspect of the object while a method accesses or uses the owning object.
    An example in pseudo-code:

    Object Ball
    Property color(some Value)
    Method bounce(subroutine describing the movement of the Ball)
    

    Where the ball is defined and given a color(property) while the method bounce is a subroutine that describes the how the ball will react on hitting another object.
    Not all languages have properties, i.e. Java only has fields that must be accessed by getters and setters.

    0 讨论(0)
提交回复
热议问题