what is a member vs. a property

2019-01-23 01:18发布

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 properties can also be objects themselves, I was left with a general description and list of exceptions.

Can somebody please lay out a good definition of when to consider something a member vs. a property? Maybe I'm bastardizing the concept, or is it just that a member is just the internal name I use, and the property is what's exposed to other objects?

I don't think that not knowing the answer to this question has affected the quality of my programming, and it's just a semantics point, but it still bothers me that I can't explain it to him.

9条回答
相关推荐>>
2楼-- · 2019-01-23 01:49

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){;}
  }
}
查看更多
Juvenile、少年°
3楼-- · 2019-01-23 02:03

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.

查看更多
放我归山
4楼-- · 2019-01-23 02:05

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.

查看更多
戒情不戒烟
5楼-- · 2019-01-23 02:07

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.)

查看更多
ゆ 、 Hurt°
6楼-- · 2019-01-23 02:07

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.

查看更多
干净又极端
7楼-- · 2019-01-23 02:13

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

查看更多
登录 后发表回答