2013.
01.
08
21:13:12
※自分用のメモなので随時追記していきます。
・public→どこからでも好きなように呼び出せる
・private→自クラス or サブクラスからしか参照できない
レシーバーをくっつけて呼べない。
・protected→自クラス or サブクラスからしか参照できない
レシーバーをくっつけて呼べる。
・可視性の変更は「可視性 :メソッド名」で指定可能
「public :hogeMethod」
「private :hogeMethod」
「protected:hogeMethod」
class Hoge
#public
def hoge1
"hoge1"
end
public :hoge1
#private
def hoge2
"hoge2"
end
private :hoge2
#protected
def hoge3
"hoge3"
end
protected :hoge3
def test1
puts hoge1
puts self.hoge1
end
def test2
puts hoge2
# これはエラーになる
# puts self.hoge2
end
def test3
puts hoge3
puts self.hoge3
end
end
aaa = Hoge.new
aaa.test1
aaa.test2
aaa.test3
aaa.hoge1
#これはエラーになる
#aaa.hoge2
#これはエラーになる
#aaa.hoge3
・可視性のデフォルトを変更したければ
「public」「private」「protected」をコード内に記述
class Hoge
#デフォルトをpublicに変更
public
def hoge4
"hoge4"
end
#デフォルトをprivateに変更
private
def hoge5
"hoge5"
end
#デフォルトをprotectedに変更
protected
def hoge6
"hoge6"
end
#デフォルトをpublicに戻す
public
def test4
puts hoge4
puts self.hoge4
end
def test5
puts hoge5
# これはエラーになる
# puts self.hoge5
end
def test6
puts hoge6
puts self.hoge6
end
end
aaa = Hoge.new
aaa.test4
aaa.test5
aaa.test6
aaa.hoge4
#これはエラーになる
#aaa.hoge5
#これはエラーになる
#aaa.hoge6
・メソッドをクラスの外側で定義するとprivate扱い
#private
def hoge7
"hoge7"
end
class Hoge
def test7
puts hoge7
#これはエラーになる
# puts self.hoge7
end
end
aaa = Hoge.new
aaa.test7
#これはエラーになる
#aaa.hoge7
puts hoge7
・メソッドをクラスの内側で定義するとpublic扱い
class Hoge
#public
def hoge8
"hoge8"
end
def test8
puts hoge8
puts self.hoge8
end
end
aaa = Hoge.new
aaa.test8
aaa.hoge8
ちょっと癖がありますね(^^;
privateでもサブクラスから呼べるので注意(--)b
Javaとかに慣れていると違和感あるカモです。
・public→どこからでも好きなように呼び出せる
・private→自クラス or サブクラスからしか参照できない
レシーバーをくっつけて呼べない。
・protected→自クラス or サブクラスからしか参照できない
レシーバーをくっつけて呼べる。
・可視性の変更は「可視性 :メソッド名」で指定可能
「public :hogeMethod」
「private :hogeMethod」
「protected:hogeMethod」
class Hoge
#public
def hoge1
"hoge1"
end
public :hoge1
#private
def hoge2
"hoge2"
end
private :hoge2
#protected
def hoge3
"hoge3"
end
protected :hoge3
def test1
puts hoge1
puts self.hoge1
end
def test2
puts hoge2
# これはエラーになる
# puts self.hoge2
end
def test3
puts hoge3
puts self.hoge3
end
end
aaa = Hoge.new
aaa.test1
aaa.test2
aaa.test3
aaa.hoge1
#これはエラーになる
#aaa.hoge2
#これはエラーになる
#aaa.hoge3
・可視性のデフォルトを変更したければ
「public」「private」「protected」をコード内に記述
class Hoge
#デフォルトをpublicに変更
public
def hoge4
"hoge4"
end
#デフォルトをprivateに変更
private
def hoge5
"hoge5"
end
#デフォルトをprotectedに変更
protected
def hoge6
"hoge6"
end
#デフォルトをpublicに戻す
public
def test4
puts hoge4
puts self.hoge4
end
def test5
puts hoge5
# これはエラーになる
# puts self.hoge5
end
def test6
puts hoge6
puts self.hoge6
end
end
aaa = Hoge.new
aaa.test4
aaa.test5
aaa.test6
aaa.hoge4
#これはエラーになる
#aaa.hoge5
#これはエラーになる
#aaa.hoge6
・メソッドをクラスの外側で定義するとprivate扱い
#private
def hoge7
"hoge7"
end
class Hoge
def test7
puts hoge7
#これはエラーになる
# puts self.hoge7
end
end
aaa = Hoge.new
aaa.test7
#これはエラーになる
#aaa.hoge7
puts hoge7
・メソッドをクラスの内側で定義するとpublic扱い
class Hoge
#public
def hoge8
"hoge8"
end
def test8
puts hoge8
puts self.hoge8
end
end
aaa = Hoge.new
aaa.test8
aaa.hoge8
ちょっと癖がありますね(^^;
privateでもサブクラスから呼べるので注意(--)b
Javaとかに慣れていると違和感あるカモです。