fbpx
维基百科

成员变量

面向对象程序设计中,成员变量(有时称为成员字段)是与特定对象相关联的变量,并且特定对象的所有方法(成员函数)均能访问該變量。[1]

例子

C++

class Foo {  int bar; // Member variable  public:  void setBar(const int newBar) {   bar = newBar;  } }; int main () {  Foo rect; // Local variable  return 0; } 

Java

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

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

(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) 

参考文献

  1. ^ Richard G. Baldwin. . Richard G Baldwin Programming Tutorials. 1999-03-10 [2011-08-12]. (原始内容存档于2022-04-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. 

成员变量, 在面向对象程序设计中, 有时称为成员字段, 是与特定对象相关联的变量, 并且特定对象的所有方法, 成员函数, 均能访问該變量, 目录, 例子, java, python, common, lisp, ruby, 参考文献例子, 编辑c, 编辑, class, member, variable, public, void, setbar, const, newbar, newbar, main, rect, local, variable, return, java, 编辑, public, class,. 在面向对象程序设计中 成员变量 有时称为成员字段 是与特定对象相关联的变量 并且特定对象的所有方法 成员函数 均能访问該變量 1 目录 1 例子 1 1 C 1 2 Java 1 3 Python 1 4 Common Lisp 1 5 Ruby 1 6 PHP 1 7 Lua 2 参考文献例子 编辑C 编辑 class Foo int bar Member variable public void setBar const int newBar bar newBar int main Foo rect Local variable return 0 Java 编辑 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 编辑 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 编辑 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 gt true class Poodle lt 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 gt false p has manners gt false PHP 编辑 lt 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 gt 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 gt foo 10 Prints 10 echo example gt 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 参考文献 编辑 Richard G Baldwin Q What is a member variable Richard G Baldwin Programming Tutorials 1999 03 10 2011 08 12 原始内容存档于2022 04 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 取自 https zh wikipedia org w index php title 成员变量 amp oldid 72379417, 维基百科,wiki,书籍,书籍,图书馆,

文章

,阅读,下载,免费,免费下载,mp3,视频,mp4,3gp, jpg,jpeg,gif,png,图片,音乐,歌曲,电影,书籍,游戏,游戏。