Ruby学习笔记_super

来源:互联网 发布:山下智久石原里美 知乎 编辑:程序博客网 时间:2024/06/06 07:04

class Persondef talk(name)print "my name is #{name}"endendclass Student < Persondef talk(name)superprint " and I`m a student.\n"endendaPerson=Person.newaPerson.talk("xiaoming")print "\n\n"aStudent=Student.newaStudent.talk("honghong")

输出:

my name is xiaoming

my name is honghong and I`m a student.


Person类的talk方法只是报告姓名。 Student类的talk方法用super来调用Person类的talk方法,报告姓名;随后又加上了一条语句,来表明身份。


出自《Ruby入门教程》  作者  张开川

0 0
原创粉丝点击