什么是成员vs.财产 [英] what is a member vs. a property
问题描述
一个不熟悉OO编程的朋友问我成员和财产之间的区别,而我很ham愧地承认我不能给他一个很好的答案.由于属性本身也可以是对象,因此我得到了一般性的描述和例外列表.
有人可以为何时考虑成员还是财产提供一个很好的定义吗?也许我正在混为一谈,或者仅仅是成员只是我使用的内部名称,而该属性就是其他对象所公开的内容?
我不认为不知道这个问题的答案会影响我的编程质量,这只是语义上的要点,但是我无法向他解释它仍然使我感到困扰.
属性是一种成员.其他可能是构造函数,方法,字段,嵌套类型,转换,索引器等-当然取决于语言/平台.在很多时候,术语的 exact 含义取决于上下文.
要根据C#3.0规范的第1.6.1节提供特定于C#的定义:
下表概述了一个类可以包含的成员类型.
(行...)
- 常量
- 字段
- 方法
- 属性
- 索引器
- 事件
- 操作员
- 构造函数
- 析构函数
- 类型
请注意,这是类的成员.不同的事物"具有不同类型的成员-例如,在C#中,接口不能将字段作为成员.
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.
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.
这篇关于什么是成员vs.财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!