=begin
class test
def def1
puts "public"
end
def def2
puts "private"
end
def def3
puts "protected"
end
def def0
def1
def2
def3
end
public :def1 ,:def0
private :def2
protected :def3
end
=begin
test.def1
test.def2
test.def3
test.def0
#=end
test = test.new
#test.def1
#test.def2
#test.def3
#test.def0
class test1 <test
end
test1 = test1.new
test1.def1
test1.def0
class song
def initialize(name, artist, duration)
@name = name
@artist = artist
@duration = duration
end
end
#asong = song.new("hello","song",124)
#puts asong.inspect
#puts asong.to_s
class karaokesong < song
def initialize(name, artist, duration, lyrics)
super(name, artist, duration)
@lyrics = lyrics
end
end
asong = karaokesong.new("my way", "sinatra", 225, "and now, the...")
puts asong.inspect # "song: my way--sinatra (225)"
class test
def one
puts "私有"
end
def two
one
puts "共有"
end
protected : one
public : two
end
test = test.new
test.two
class person
def talk
puts " public :talk, 将调用speak"
speak
end
def speak
puts "protected:speak,将调用laugh"
laugh
end
def laugh
puts " private:laugh"
end
protected :speak
private :laugh
end
p1=person.new
p1.talk
#p1.speak 实例对象不能访问 protected 方法
#p1.laugh 实例对象不能访问 private 方法
class person
def speak
"protected:speak "
end
def laugh
" private:laugh"
end
protected :speak
private :laugh
end
class student < person
def uselaugh
puts laugh
end
def usespeak
puts speak
end
end
p2=student.new
p2.uselaugh
p2.usespeak
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)
=end
class person
private #后面的方法设定为private
def talk
puts " already talk "
end
end
p1=person.new
p1.talk # private方法不能访问
class person
public :talk
end
p1.talk
class test
def def1
puts "public"
end
def def2
puts "private"
end
def def3
puts "protected"
end
def def0
def1
def2
def3
end
public :def1 ,:def0
private :def2
protected :def3
end
=begin
test.def1
test.def2
test.def3
test.def0
#=end
test = test.new
#test.def1
#test.def2
#test.def3
#test.def0
class test1 <test
end
test1 = test1.new
test1.def1
test1.def0
class song
def initialize(name, artist, duration)
@name = name
@artist = artist
@duration = duration
end
end
#asong = song.new("hello","song",124)
#puts asong.inspect
#puts asong.to_s
class karaokesong < song
def initialize(name, artist, duration, lyrics)
super(name, artist, duration)
@lyrics = lyrics
end
end
asong = karaokesong.new("my way", "sinatra", 225, "and now, the...")
puts asong.inspect # "song: my way--sinatra (225)"
class test
def one
puts "私有"
end
def two
one
puts "共有"
end
protected : one
public : two
end
test = test.new
test.two
class person
def talk
puts " public :talk, 将调用speak"
speak
end
def speak
puts "protected:speak,将调用laugh"
laugh
end
def laugh
puts " private:laugh"
end
protected :speak
private :laugh
end
p1=person.new
p1.talk
#p1.speak 实例对象不能访问 protected 方法
#p1.laugh 实例对象不能访问 private 方法
class person
def speak
"protected:speak "
end
def laugh
" private:laugh"
end
protected :speak
private :laugh
end
class student < person
def uselaugh
puts laugh
end
def usespeak
puts speak
end
end
p2=student.new
p2.uselaugh
p2.usespeak
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)
=end
class person
private #后面的方法设定为private
def talk
puts " already talk "
end
end
p1=person.new
p1.talk # private方法不能访问
class person
public :talk
end
p1.talk