what is a member vs. a property

后端 未结 9 2039
灰色年华
灰色年华 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:06

    A member (variable) is just some part of the object. A property is (I'll qualify this with "usually" - I'm not sure that it's a technically clear word that has unambiguous meaning across multiple languages) is a publicly accessible aspect of the object, e.g. through getter and setter methods.

    So while (almost always) a property is a reacheable member variable, you can have a property that's not really part of the object state (not that this is good design):

    public class Foo {
      public String getJunk()
      { return "Junk";}
    
      public void setJunk(String ignore){;}
      }
    }
    
    0 讨论(0)
  • 2020-12-15 04:10

    Properties are a way to expose fields, where fields are the actual variables. For example (C#):

    class Foo {
      private int field;
      public int Property {
        get { return field; }
        set { field = value; }
      }
    }
    0 讨论(0)
  • 2020-12-15 04:12

    A property is one kind of member. Others might be constructors, methods, fields, nested types, conversions, indexers etc - depending on the language/platform, of course. A lot of the time the exact meaning of terminology depends on the context.

    To give a C#-specific definition, from the C# 3.0 spec, section 1.6.1:

    The following table provides an overview of the kinds of members a class can contain.
    (Rows for...)

    • Constants
    • Fields
    • Methods
    • Properties
    • Indexers
    • Events
    • Operators
    • Constructors
    • Destructors
    • Types

    Note that that's members of a class. Different "things" have different kinds of members - in C#, an interface can't have a field as a member, for example.

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

    Member is a generic term (likely originated in C++, but also defined in Java) used to denote a component of a class. Property is a broad concept used to denote a particular characteristic of a class which, once the class is instantiated, will help define the object's state.

    The following passages, extracted from "Object-Oriented Analysis and Design" by Grady Booch help clarify the subject. Firstly, it's important to understand the concepts of state and behaviour:

    The state of an object encompasses all of the (usually static) properties of the object plus the current (usually dynamic) values of each of these properties. By properties, we mean the totality of the object's attributes and relationships with other objects.

    Behaviour is how an object acts and reacts, in terms of its state changes and message passing (methods); the outwardly visible and testable activity of an object.

    So, the behaviour of an object depends on the available operations and its state (properties and their current values). Note that OOP is quite generic regarding certain nomenclature, as it varies wildly from language to language:

    The terms field (Object Pascal), instance variable (Smalltalk), member object (C++), and slot (CLOS) are interchangeable, meaning a repository for part of the state of an object. Collectively, they constitute the object's structure.

    An operation upon an object, defined as part of the declaration of a class. The terms message (Smalltalk), method (many OO languages), member function (C++), and operation are usually interchangeable.

    But the notation introduced by the author is precise:

    An attribute denotes a part of an aggregate object, and so is used during analysis as well as design to express a singular property of the class. Using the language-independent syntax, an attribute may have a name, a class, or both, and optionally a default expression: A:C=E.

    An operation denotes some service provided by the class. Operations (...) are distinguished from attributes by appending parentheses or by providing the operation's complete signature, composed of return class, name, and formal arguments (if any): R N(Arguments)

    In summary, you can think of members as everything that composes the class, and properties as the members (attributes) that collectively define the structure of the class, plus its relationships to other classes. When the class is instantiated, values are assigned to its properties in order to define the object's state.

    Cheers

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

    from PHP manual:

    Class member variables are called "properties". You may also see them referred to using other terms such as "attributes" or "fields". They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization.

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

    Neither of the two terms has any defined meaning whatsoever in Object-Oriented Programming or Object-Oriented Design. Nor do they have any defined meaning in the overwhelming majority of programming languages.

    Only a very small number of programming languages have a concept called property or member, and even fewer have both.

    Some examples of languages that have either one of the two are C++ (which has members), ECMAScript (which has properties) and C# (which has both). However, these terms don't necessarily denote the same concepts in different programming languages. For example, the term "member" means roughly the same thing in C++ and C#, but the term "property" means completely different things in ECMAScript and C#. In fact, the term "property" in ECMAScript denotes roughly the same concept (ie. means roughly the same thing) as the term "member" in C++ and C#.

    All this is just to say that those two terms mean exactly what the relevant specification for the programming language says they mean, no more and no less. (Insert gratuitous Lewis Carroll quote here.)

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