メンバー変数

オブジェクト指向プログラミングではメンバー変数(メンバーフィールドと呼ばれることもあります) は、特定のオブジェクトに関連付けられ、そのすべてのメソッド(メンバー関数) からアクセスできる変数です。

クラスベースのプログラミング言語では、これらは2つの種類に区別されます。クラス変数静的メンバー変数とも呼ばれます)では、変数のコピーが1つだけクラスのすべてのインスタンスで共有されます。インスタンス変数では、クラスの各インスタンスが独自の変数のコピーを持ちます。[1]

C++

class Foo { int bar ; // メンバー変数public : void setBar ( const int newBar ) { bar = newBar ; } };                int main () { Foo rect ; // ローカル変数       0を返す; } 

ジャワ

public class Program { public static void main ( String [] args ) { // これはローカル変数です。その有効期間は// レキシカルスコープによって決定されます。Foo foo ; } }             public class Foo { /* これはメンバー変数です。Fooの新しいインスタンス ごとに、この変数の新しいインスタンスが生成されます 。この変数の寿命は、 Fooの「この」インスタンス の寿命と同じです 。 */    intバー; } 

パイソン

クラスFoo : def __init__ ( self ): self . _bar = 0       @property  def bar ( self ): self . _bar を返します    @bar .セッター def bar ( self , new_bar ): self . _bar = new_bar     f  =  Foo () f . bar  =  100を印刷します( f . bar )

コモンリスプ

( defclass foo () ( bar )) ( defvar f ( make-instance 'foo )) ( setf ( slot-value f 'bar ) 100 ) ( print ( slot-value f 'bar ))                

ルビー

/*  Ruby には、クラス、クラスインスタンス、インスタンスの 3 つのメンバー変数型があります。*/クラス  # クラス変数はクラス本体内で 2 つのアットマークで定義され、すべての犬とそこから派生した犬種 (ある場合) に関するデータを記述します@@sniffs = true    終わりmutt = Dog .新しいmutt .クラス. sniffs #=> true   クラスプードル<    # 「クラスインスタンス変数」はクラス本体内でアットマーク1つで定義され、Poodleクラスに関するデータのみを記述します。親クラスやPoodleから派生したサブクラスについては何も記述しません。 @sheds = false      # 新しいPoodleインスタンスが作成されると、デフォルトでは未訓練状態になります。'trained'変数はinitializeメソッド内でローカルであり、インスタンス変数@trainedを設定するために使用されます。# インスタンス変数はインスタンスメソッド内で定義され、Poodleインスタンスのメンバーです。def initialize ( trained = false ) @trained = training end           def has_manners? @trained終了   終わりp = Poodle . new p . class . sheds #=> false p . has_manners? #=> false    

PHP

<?phpclass  Example {  /**  * インスタンスメンバー変数の例。 *  * メンバー変数は、public、protected、またはprivateにすることができます。 *  * @var int  */  public  int  $foo ; /**  * 静的メンバー変数の例。 *  * @var bool  */  protected  static  int  $bar ; /**  * コンストラクターメソッドの例。 *  * @param int $foo  */  public  function  __construct ( int  $foo )  {  // foo を設定します。 $this -> foo  =  $foo ;  } }// 新しい Example オブジェクトを作成します。// "foo" メンバー変数を 5 に設定します。$example  =  new  Example ( 5 );// "foo" メンバー変数を 10 に上書きします。$example -> foo  =  10 ;// 10 を出力します。echo $  example -> foo ;

ルア

--region example --- @class example_c --- @field foo number 例 "メンバー変数". local example_c = {} local example_mt = { __index = example_c }        --- 例からオブジェクトを作成します。--- @return example_c function example_c . new ( foo ) -- 最初のテーブル引数はオブジェクトのメンバー変数です。-- Lua オブジェクトではメタテーブルであり、そのメンバー変数はテーブルのキーと値のペアです。return setmetatable ({ foo = foo }, example_mt ) end --endregion          -- サンプルオブジェクトを作成します。-- メンバー変数「foo」を5に設定します。local example = example_c . new ( 5 )   -- メンバー変数「foo」を 10 に上書きします。: foo = 10  -- 10 を出力します。print ( example . foo )

参照

参考文献

  1. ^ Richard G. Baldwin (1999-03-10). 「Q - メンバー変数とは何ですか?」. Richard G. Baldwin プログラミングチュートリアル. 2011-08-12閲覧.メンバー変数は、クラスのメンバー(クラス変数)またはそのクラスからインスタンス化されたオブジェクトのメンバー(インスタンス変数)です。クラス内で宣言する必要がありますが、クラスのメソッド本体内では宣言できません。
「https://en.wikipedia.org/w/index.php?title=Member_variable&oldid=1317280864」から取得