赛派号

最好的颈椎腰椎按摩仪器排名 Member variable

Variable associated with a specific object, and accessible for all its methods

In object-oriented programming, a member variable (sometimes called a member field) is a variable that is associated with a specific object, and accessible for all its methods (member functions).

In class-based programming languages, these are distinguished into two types: class variables (also called static member variables), where only one copy of the variable is shared with all instances of the class; and instance variables, where each instance of the class has its own independent copy of the variable.[1]

Examples[edit] C++[edit] class Foo { int bar; // Member variable public: void setBar(const int newBar) { bar = newBar; } }; int main () { Foo rect; // Local variable return 0; } Ja[edit] public class Program { public static void main(String[] args) { // This is a local variable. Its lifespan // is determined by lexical scope. Foo foo; } } public class Foo { /* This is a member variable - a new instance of this variable will be created for each new instance of Foo. The lifespan of this variable is equal to the lifespan of "this" instance of Foo */ int bar; } Python[edit] class Foo: def __init__(self): self._bar = 0 @property def bar(self): return self._bar @bar.setter def bar(self, new_bar): self._bar = new_bar f = Foo() f.bar = 100 print(f.bar) Common Lisp[edit] (defclass foo () (bar)) (defvar f (make-instance 'foo)) (setf (slot-value f 'bar) 100) (print (slot-value f 'bar)) Ruby[edit] /* Ruby has three member variable types: class, class instance, and instance. */ class Dog # The class variable is defined within the class body with two at-signs # and describes data about all Dogs *and* their derived Dog breeds (if any) @@sniffs = true end mutt = Dog.new mutt.class.sniffs #=> true class Poodle false p.has_manners? #=> false PHP[edit]

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lsinopec@gmail.com举报,一经查实,本站将立刻删除。

上一篇 没有了

下一篇没有了