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入门教程》  作者  张开川

你可能感兴趣的:(Ruby学习笔记_super)