词条 | Instance variable |
释义 |
}}{{distinguish|class variable}} In object-oriented programming with classes, an instance variable is a variable defined in a class (i.e. a member variable ), for which each instantiated object of the class has a separate copy, or instance. An instance variable is similar to a class variable.[1] An instance variable is a variable which is declared in a class but outside the constructor and the method/function. Instance variables are created when an object is instantiated, and are accessible to all the methods, the constructor and block in the class. Access modifiers can be given to the instance variable. An instance variable is not a class variable although there are similarities. It is a type of class attribute (or class property, field, or data member). The same dichotomy between instance and class members applies to methods ("member functions") as well; a class may have both instance methods and class methods. Each instance variable lives in memory for the life of the object it is owned by.[2] Variables are properties an object knows about itself. All instances of an object have their own copies of instance variables, even if the value is the same from one object to another. One object instance can change values of its instance variables without affecting all other instances. Instance variables can be used by all methods of a class unless the method is declared as static.[3] ExampleIn this C++ example, the instance variable References1. ^{{cite web|title=The Java Tutorial, Variables|url=http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html|website=docs.oracle.com|publisher=Oracle|accessdate=23 October 2014|archiveurl=https://web.archive.org/web/20141023153904/http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html|archivedate=23 October 2014}} {{DEFAULTSORT:Instance Variable}}{{Compu-prog-stub}}2. ^{{cite web|title=The Java Tutorials, Understanding Class Members|url=http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html|website=docs.oracle.com|publisher=Oracle|accessdate=23 October 2014|archiveurl=https://web.archive.org/web/20141011000515/http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html|archivedate=11 October 2014}} 3. ^{{cite web|last1=Matuszek|first1=David|title=Static|url=http://www.cis.upenn.edu/~matuszek/General/JavaSyntax/static.html|website=cis.upenn.edu|publisher=University of Pennsylvania|accessdate=23 October 2014|archiveurl=https://web.archive.org/web/20141023160745/http://www.cis.upenn.edu/~matuszek/General/JavaSyntax/static.html|archivedate=23 October 2014}} 2 : Object-oriented programming|Variable (computer science) |
随便看 |
|
开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。