ruby public protected private
时间:2010-07-15 来源:zhoujiabin810812
class Person
def speak
"protected:speak "
end
def laugh
" private:laugh"
end
protected :speak
private :laugh
def useLaugh(another)
puts another.laugh
end
def useSpeak(another)
puts another.speak
end
end
p1=Person.new
p2=Person.new
p2.useSpeak(p1)
#p2.useLaugh(p1)
当p2.useLaugh(p1)执行的时候将会报错。
def speak
"protected:speak "
end
def laugh
" private:laugh"
end
protected :speak
private :laugh
def useLaugh(another)
puts another.laugh
end
def useSpeak(another)
puts another.speak
end
end
p1=Person.new
p2=Person.new
p2.useSpeak(p1)
#p2.useLaugh(p1)
当p2.useLaugh(p1)执行的时候将会报错。
相关阅读 更多 +
排行榜 更多 +