5.特異クラス
class Foo
def hello
print "hello.\n"
end
end
foo = Foo.new
foo.hello #=>hello.
class << foo
attr :name, true
def hello
print "hello. I'm ", @name, ".\n"
end
end
foo.name = "Tom"
foo.hello #=>hello. I'm Tom.
参考
○Rubyとオブジェクト指向についてアウトプットしてみる○
1.メッセージ ~オブジェクトは「問いかけたらきちんと返事をしてくれる」~
2.継承とは
3.メソッドの実行
4.特異メソッド
5.特異クラス
6.オーバーライド
7.Mixin(include)
8.Mixin(extend)
9.委譲