请输入您要查询的百科知识:

 

词条 Instance variable
释义

  1. Example

  2. References

{{multiple issues|{{expert-subject|1=Computer science|date=September 2013}}{{original research|date=September 2013}}{{tone|date=September 2013}}
}}{{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]

Example

struct Request {

    static int count1; // variable name is not of importance    int number;
    Request() {        number = count1; // modifies the instance variable "this->number"        ++count1; // modifies the class variable "Request::count1"    }

};

int Request::count1 = 0;

In this C++ example, the instance variable Request::number is a copy of the class variable Request::count1 where each instance constructed is assigned a sequential value of count1 before it is incremented. Since number is an instance variable, each Request object contains its own distinct value; in contrast, there is only one object Request::count1 available to all instances with the same value.

References

1. ^{{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}}
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}}
{{DEFAULTSORT:Instance Variable}}{{Compu-prog-stub}}

2 : Object-oriented programming|Variable (computer science)

随便看

 

开放百科全书收录14589846条英语、德语、日语等多语种百科知识,基本涵盖了大多数领域的百科知识,是一部内容自由、开放的电子版国际百科全书。

 

Copyright © 2023 OENC.NET All Rights Reserved
京ICP备2021023879号 更新时间:2024/11/12 7:36:52