Member variableIn 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] ExamplesC++class Foo {
int bar; // Member variable
public:
void setBar(const int newBar) {
bar = newBar;
}
};
int main () {
Foo rect; // Local variable
return 0;
}
Javapublic 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;
}
Pythonclass 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(defclass foo () (bar))
(defvar f (make-instance 'foo))
(setf (slot-value f 'bar) 100)
(print (slot-value f 'bar))
Ruby/*
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 < 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 instance
def initialize(trained = false)
@trained = trained
end
def has_manners?
@trained
end
end
p = Poodle.new
p.class.sheds #=> false
p.has_manners? #=> false
PHP<?php
class Example
{
/**
* Example instance member variable.
*
* Member variables may be public, protected or private.
*
* @var int
*/
public int $foo;
/**
* Example static member variable.
*
* @var bool
*/
protected static int $bar;
/**
* Example constructor method.
*
* @param int $foo
*/
public function __construct(int $foo)
{
// Sets foo.
$this->foo = $foo;
}
}
// Create a new Example object.
// Set the "foo" member variable to 5.
$example = new Example(5);
// Overwrite the "foo" member variable to 10.
$example->foo = 10;
// Prints 10.
echo $example->foo;
Lua--region example
--- @class example_c
--- @field foo number Example "member variable".
local example_c = {}
local example_mt = {__index = example_c}
--- Creates an object from example.
--- @return example_c
function example_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.
return setmetatable({
foo = foo
}, example_mt)
end
--endregion
-- Create an example object.
-- Set the "foo" member variable to 5.
local example = example_c.new(5)
-- Overwrite the "foo" member variable to 10.
example.foo = 10
-- Prints 10.
print(example.foo)
See alsoReferences
|
Index:
pl ar de en es fr it arz nl ja pt ceb sv uk vi war zh ru af ast az bg zh-min-nan bn be ca cs cy da et el eo eu fa gl ko hi hr id he ka la lv lt hu mk ms min no nn ce uz kk ro simple sk sl sr sh fi ta tt th tg azb tr ur zh-yue hy my ace als am an hyw ban bjn map-bms ba be-tarask bcl bpy bar bs br cv nv eml hif fo fy ga gd gu hak ha hsb io ig ilo ia ie os is jv kn ht ku ckb ky mrj lb lij li lmo mai mg ml zh-classical mr xmf mzn cdo mn nap new ne frr oc mhr or as pa pnb ps pms nds crh qu sa sah sco sq scn si sd szl su sw tl shn te bug vec vo wa wuu yi yo diq bat-smg zu lad kbd ang smn ab roa-rup frp arc gn av ay bh bi bo bxr cbk-zam co za dag ary se pdc dv dsb myv ext fur gv gag inh ki glk gan guw xal haw rw kbp pam csb kw km kv koi kg gom ks gcr lo lbe ltg lez nia ln jbo lg mt mi tw mwl mdf mnw nqo fj nah na nds-nl nrm nov om pi pag pap pfl pcd krc kaa ksh rm rue sm sat sc trv stq nso sn cu so srn kab roa-tara tet tpi to chr tum tk tyv udm ug vep fiu-vro vls wo xh zea ty ak bm ch ny ee ff got iu ik kl mad cr pih ami pwn pnt dz rmy rn sg st tn ss ti din chy ts kcg ve
Portal di Ensiklopedia Dunia