「Ruby入門」クラス変数(@@変数名)のサンプル

1.クラスの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
class Sample
def setAddr(str)
@@addr = str
end
def getAddr()
return @@addr
end
end
class Sample def setAddr(str) @@addr = str end def getAddr() return @@addr end end
class Sample
  def setAddr(str)
    @@addr = str
  end

  def getAddr()
    return @@addr
  end
end

2.クラスのインスタンス作成
cftA = Sample.new()
cftB = Sample.new()
cftC = Sample.new()

3.クラス変数参照
cftA.setAddr(“Address01")
puts cftA.getAddr()
puts cftB.getAddr()
puts cftC.getAddr()

4.実行結果
Address01
Address01
Address01

Ruby

Posted by arkgame