class Student < Person

来源:互联网 发布:暴风影音mac版使用说明 编辑:程序博客网 时间:2024/06/07 20:49
# encoding: utf-8#E4.4-1.rb ,接着上一节的代码,添加如下:class Persondef initialize( name, age=18 )@name = name@age = age@motherland = "China"end #初始化方法结束def talkputs "my name is "+@name+", age is "+@age.to_sif @motherland == "China"puts "I am a Chinese."elseputs "I am a foreigner."endend # talk方法结束attr_writer :motherlandend # Person类结束class Student < Persondef talkputs "I am a student. my name is "+@name+", age is "+@age.to_send # talk方法结束end # Student类结束p3=Student.new("kaichuan",25); p3.talkp4=Student.new("Ben"); p4.talk=beginI am a student. my name is kaichuan, age is 25I am a student. my name is Ben, age is 18请按任意键继续. . .=end

原创粉丝点击