Ruby學習筆記_super

class Person
	def talk(name)
		print "my name is #{name}"
	end
end

class Student < Person
	def talk(name)
		super
		print " and I`m a student.\n"
	end
end

aPerson=Person.new
aPerson.talk("xiaoming")
print "\n\n"

aStudent=Student.new
aStudent.talk("honghong")

輸出:

my name is xiaoming

my name is honghong and I`m a student.


Person類的talk方法只是報告姓名。 Student類的talk方法用super來調用Person類的talk方法,報告姓名;隨後又加上了一條語句,來表明身份。


出自《Ruby入門教程》  作者  張開川

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章