属性和实例变量有什么区别?
我想我一直在互换/错误地使用这些术语!
I think I've been using these terms interchangably / wrongly!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我想我一直在互换/错误地使用这些术语!
I think I've been using these terms interchangably / wrongly!
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(10)
属性是与对象关联的某种数据。 例如,圆的一个属性是它的直径,另一个属性是它的面积。
实例变量是存储在对象内的一段数据。 它不一定需要直接与属性相对应。 例如(呵呵),一个圆可以将其半径存储在实例变量中,并根据该半径计算其直径和面积。 所有三个仍然是属性,但只有半径存储在实例变量中。
某些语言具有“第一类”属性的概念。 这意味着对于客户端应用程序来说,该属性看起来和使用起来就像一个实例变量。 也就是说,您可以编写
circle.diameter
,而不是circle.getDiameter()
之类的内容,而不是circle.setRadius(5)
>,您可以编写circle.radius = 5
。A property is some sort of data associated with an object. For instance, a property of a circle is its diameter, and another is its area.
An instance variable is a piece of data that is stored within an object. It doesn't necessarily need to correspond directly with a property. For instance (heh), a circle may store its radius in an instance variable, and calculate its diameter and area based on that radius. All three are still properties, but only the radius is stored in an instance variable.
Some languages have the concept of "first class" properties. This means that to a client application, the property looks and is used like an instance variable. That is, instead of writing something like
circle.getDiameter()
, you would writecircle.diameter
, and instead ofcircle.setRadius(5)
, you would writecircle.radius = 5
.与给出的其他答案相反,我确实认为成员变量和与语言无关的属性之间存在有用的区别。
这种区别在面向组件的编程中最为明显,它在任何地方都很有用,但在图形 UI 中最容易理解。 在这种情况下,我倾向于将组件的设计时配置视为操纵对象的“属性”。 例如,我通过设置文本输入字段的属性来选择文本输入字段的前景色和背景颜色、边框样式和字体。 虽然这些属性可以在运行时更改,但通常不会。 在运行时,代表字段内容的一组不同变量更有可能被读取和写入。 我认为这些信息是组件的“状态”。
为什么这种区别有用? 当创建将组件连接在一起的抽象时,通常只需要公开“状态”变量。 回到文本字段示例,您可以声明一个提供对当前内容的访问的接口。 但是控制组件外观的“属性”仅在具体实现类上定义。
In contrast to the other answers given, I do think that there is a useful distinction between member variables and properties that is language-agnostic.
The distinction is most apparent in component-oriented programming, which is useful anywhere, but easiest to understand in a graphical UI. In that context, I tend to think of the design-time configuration of a component as manipulating the "properties" of an object. For example, I choose the foreground and background colors, the border style, and font of a text input field by setting its properties. While these properties could be changed at runtime, they typically aren't. At runtime, a different set of variables, representing the content of the field, are much more likely to be read and written. I think of this information as the "state" of the component.
Why is this distinction useful? When creating an abstraction for wiring components together, usually only the "state" variables need to be exposed. Going back to the text field example, you might declare an interface that provides access to the current content. But the "properties" that control the look and feel of the component are only defined on a concrete implementation class.
伊恩,这基本上是一个术语问题,尽管与该问题相关的“语言不可知”标签,但与语言/环境非常相关。
出于设计讨论的目的,属性和实例变量可以互换使用,因为属性是描述对象的数据项。
当谈论特定语言时,这两者可能不同。 例如,在 C# 中,属性实际上是返回对象的函数,而实例变量是类的非静态成员变量。
Iain, this is basically a terminology question and is, despite the "language-agnostic" tag associated with this question, very language/environment related.
For design discussions sake, property and instance variable can be used interchangeably, since the idea is that a property is a data item describing an object.
When talking about a specific language these two can be different. For example, in C# a property is actually a function that returns an object, while an instance variable is a non-static member variable of a class.
Hershi 的说法是正确的,这是针对特定语言的。 但要添加到特定于语言的答案:
在Python中,实例变量是实例的属性,(通常)在实例的字典中引用的东西。 这类似于 Java 中的成员或实例变量,只不过所有内容都是公共的。
属性是 getter/setter 方法的快捷方式,看起来就像实例变量。 因此,在下面的类定义中(修改自 Guido 的 新样式对象宣言):
y
是z
的实例变量,x
是属性。 (一般来说,在定义属性的地方,会使用一些技术来隐藏关联的实例变量,以便其他代码不能直接访问它。)Python 中属性的好处是设计者不必到处寻找先发制人地封装所有实例变量,因为将来通过将实例变量转换为属性进行的封装不应破坏任何现有代码(除非代码利用了封装试图修复的漏洞,或者依赖于类检查或其他元数据) - 编程技术)。所有这些都是一个很长的答案,说在设计层面,谈论属性是件好事。 您可能需要执行什么类型的封装是不可知的。 我想这个原则与语言无关,但确实适用于 python 以外的语言。
Hershi is right about this being language specific. But to add to the trail of language specific answers:
In python, an instance variable is an attribute of an instance, (generally) something that is referred to in the instance's dictionary. This is analogous to members or instance variables in Java, except everything is public.
Properties are shortcuts to getter/setter methods that look just like an instance variable. Thus, in the following class definition (modified from Guido's new style object manifesto):
y
is an instance variable ofz
,x
is a property. (In general, where a property is defined, there are some techniques used to obscure the associated instance variable so that other code doesn't directly access it.) The benefit of properties in python is that a designer doesn't have to go around pre-emptively encapsulating all instance variables, since future encapsulation by converting an instance variable to a property should not break any existing code (unless the code is taking advantage of loopholes your encapsulation is trying to fix, or relying on class inspection or some other meta-programming technique).All this is a very long answer to say that at the design level, it's good to talk about properties. It is agnostic as to what type of encapsulation you may need to perform. I guess this principle isn't language agnostic, but does apply to languages beside python.
用 C# 完成的代码示例
code example done in C#
也许那是因为你最初是从 C++ 来的吧?!
在我的学生时代,我的教授一直在说阶级属性或阶级属性。 自从我进入 Java C# 世界以来,我开始听说成员。 类成员、实例成员...
然后属性出现! 在 Java 和 .NET 中。 所以我认为你最好称其为会员。 无论它们是实例成员(或您所说的实例变量)还是类成员......
干杯!
Maybe thats because you first came from C++ right?!
In my school days I had professors that said class properties or class atributes all the time. Since I moved to the Java C# world, I started hearing about members. Class members, instance members...
And then Properties apear! in Java and .NET. So I think its better for you to call it members. Wheather they are instance members (or as you called it instance variable) or class Members....
Cheers!
属性可以(我想大多数情况下都可以)返回实例变量,但它可以做更多的事情。 您可以将逻辑放入属性中、聚合值或更新其他实例变量等。但我认为最好避免这样做。 逻辑应该体现在方法中。
A property can, and I suppose mostly does, return an instance variable but it can do more. You could put logic in a property, aggregate values or update other instance variables etc. I think it is best to avoid doing so however. Logic should go into methods.
在 Java 中,我们有一个叫做 JavaBeans Properties 的东西,但是它基本上是一个实例变量,其 getter 和 setter 遵循特定的命名模式。
In Java we have something called JavaBeans Properties, but that is basically a instance variable that follows a certain naming pattern for its getter and setter.
在 Objective C 中,属性是一个实例变量,它可以利用重载的点运算符来调用其 setter 和 getter。 所以 my.food = "cheeseburger" 实际上被解释为 [my setFood:"cheeseburger"]。 这是另一种情况,其中定义绝对与语言无关,因为 Objective-c 定义了 @property 关键字。
In objective c, a property is an instance variable which can take advantage of an overloaded dot operator to call its setter and getter. So my.food = "cheeseburger" is actually interpreted as [my setFood:"cheeseburger"]. This is another case where the definition is definitely not language agnostic because objective-c defines the @property keyword.
除此之外,在 C# 等语言中,属性本质上是一个 get 和 set 函数。 因此,除了获取/设置之外,它还可以运行自定义逻辑。 实例变量无法做到这一点。
At add to what has been said, in a langauge like C#, a property is essentially a get and set function. As a result, it can have custom logic that runs in addition to the getting/setting. An instance variable cannot do this.