Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Member variable

From Wikipedia, the free encyclopedia
Variable associated with a specific object, and accessible for all its methods

Inobject-oriented programming, amember variable (sometimes called amemberfield) is avariable that is associated with a specificobject, and accessible for all itsmethods (member functions).

Inclass-based programming languages, these are distinguished into two types:class variables (also calledstatic member variables), where only one copy of the variable is shared with allinstances of theclass; andinstance variables, where each instance of the class has its own independent copy of the variable.[1]

Examples

[edit]

C++

[edit]
classFoo{intbar;// Member variablepublic:voidsetBar(constintnewBar){bar=newBar;}};intmain(){Foorect;// Local variablereturn0;}

Java

[edit]
publicclassProgram{publicstaticvoidmain(String[]args){// This is a local variable. Its lifespan// is determined by lexical scope.Foofoo;}}publicclassFoo{/* 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    */intbar;}

Python

[edit]
classFoo:def__init__(self):self._bar=0@propertydefbar(self):returnself._bar@bar.setterdefbar(self,new_bar):self._bar=new_barf=Foo()f.bar=100print(f.bar)

Common Lisp

[edit]
(defclassfoo()(bar))(defvarf(make-instance'foo))(setf(slot-valuef'bar)100)(print(slot-valuef'bar))

Ruby

[edit]
/*  Ruby has three member variable types: class, class instance, and instance.*/classDog# 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=trueendmutt=Dog.newmutt.class.sniffs#=> trueclassPoodle<Dog# The "class instance variable" is defined within the class body with a single at-sign# and describes data about only the Poodle class. It makes no claim about its parent class# or any possible subclass derived from Poodle@sheds=false# When a new Poodle instance is created, by default it is untrained. The 'trained' variable# is local to the initialize method and is used to set the instance variable @trained# An instance variable is defined within an instance method and is a member of the Poodle instancedefinitialize(trained=false)@trained=trainedenddefhas_manners?@trainedendendp=Poodle.newp.class.sheds#=> falsep.has_manners?#=> false

PHP

[edit]
<?phpclassExample{/**     * Example instance member variable.     *     * Member variables may be public, protected or private.     *     * @var int     */publicint$foo;/**     * Example static member variable.     *     * @var bool     */protectedstaticint$bar;/**     * Example constructor method.     *     * @param int $foo     */publicfunction__construct(int$foo){// Sets foo.$this->foo=$foo;}}// Create a new Example object.// Set the "foo" member variable to 5.$example=newExample(5);// Overwrite the "foo" member variable to 10.$example->foo=10;// Prints 10.echo$example->foo;

Lua

[edit]
--region example--- @class example_c--- @field foo number Example "member variable".localexample_c={}localexample_mt={__index=example_c}--- Creates an object from example.--- @return example_cfunctionexample_c.new(foo)-- The first table argument is our object's member variables.-- In a Lua object is a metatable and its member variables are table key-value pairs.returnsetmetatable({foo=foo},example_mt)end--endregion-- Create an example object.-- Set the "foo" member variable to 5.localexample=example_c.new(5)-- Overwrite the "foo" member variable to 10.example.foo=10-- Prints 10.print(example.foo)

See also

[edit]

References

[edit]
  1. ^Richard G. Baldwin (1999-03-10)."Q - What is a member variable?". Richard G Baldwin Programming Tutorials. Retrieved2011-08-12.A member variable is a member of a class (class variable) or a member of an object instantiated from that class (instance variable). It must be declared within a class, but not within the body of a method of the class.
Retrieved from "https://en.wikipedia.org/w/index.php?title=Member_variable&oldid=1269130348"
Categories:
Hidden categories:

[8]ページ先頭

©2009-2025 Movatter.jp