| 情報学部 | 菅沼ホーム | 目次 | 索引 |
class Test def initialize(x = 0) @_x = x end end obj1 = Test.new(1) p obj1._x # エラー obj1._x = 10 # エラー p obj1._x # エラー
class Test def initialize(x = 0) @_x = x end def _x @_x end def _x=(val) @_x = val end end obj1 = Test.new(1) p obj1._x # 1 を出力 obj1._x = 10 p obj1._x # 10 を出力
class Test
def initialize(x = 0)
@_x = x
end
attr("_x", true)
end
obj1 = Test.new(1)
p obj1._x # 1 を出力
obj1._x = 10
p obj1._x # 10 を出力
class Test
def initialize(x = 0)
@_x = x
end
attr_accessor("_x")
end
obj1 = Test.new(1)
p obj1._x # 1 を出力
obj1._x = 10
p obj1._x # 10 を出力
class Test
def initialize(x = 0)
@_x = x
end
attr_reader("_x")
attr_writer("_x")
end
obj1 = Test.new(1)
p obj1._x # 1 を出力
obj1._x = 10
p obj1._x # 10 を出力
class Test
def initialize(x = 0)
@_x = x
end
attr_reader("_x")
attr_writer("_x")
end
obj1 = Test.new(1)
p obj1._x # 1 を出力
obj1._x = 10
p obj1._x # 10 を出力
| 情報学部 | 菅沼ホーム | 目次 | 索引 |