博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
RUBY的类封装,继承,多态简单演示
阅读量:6439 次
发布时间:2019-06-23

本文共 2351 字,大约阅读时间需要 7 分钟。

复制代码
class Person    def initialize(name,age=18)        @name=name        @age=age        @motherland="China"    end    def talk        puts "my name is " +@name+",age is "+@age.to_s        if @motherland == "China"            puts "I am a China."            else                puts "I am a foreigner."                end            end            attr_writer:motherland            end        p1=Person.new("Kaichuan",20)        p1.talk        p2=Person.new("Ben")        p2.motherland="ABC"        p2.talk                class Student < Person            def talk                puts "I am a student. my name is " +@name+",age is " +@age.to_s            end        end        p3=Student.new("Chengang","25");        p3.talk        p4=Student.new("Ben");        p4.talk                class Worker < Person            def talk                puts "I am a worker. my name is " +@name +",age is " + @age.to_s            end        end        p5=Worker.new("Hiong",30)        p5.talk        p6=Worker.new("Ben")        p6.talk                            class Person    def initialize(name,age=18)        @name=name        @age=age        @motherland="China"    end    def talk        puts "my name is " +@name+",age is "+@age.to_s        if @motherland == "China"            puts "I am a China."            else                puts "I am a foreigner."                end            end            attr_writer:motherland            end        p1=Person.new("Kaichuan",20)        p1.talk        p2=Person.new("Ben")        p2.motherland="ABC"        p2.talk                class Student < Person            def talk                puts "I am a student. my name is " +@name+",age is " +@age.to_s            end        end        p3=Student.new("Chengang","25");        p3.talk        p4=Student.new("Ben");        p4.talk                class Worker < Person            def talk                puts "I am a worker. my name is " +@name +",age is " + @age.to_s            end        end        p5=Worker.new("Hiong",30)        p5.talk        p6=Worker.new("Ben")        p6.talk
复制代码

>ruby hello.rb

my name is Kaichuan,age is 20
I am a China.
my name is Ben,age is 18
I am a foreigner.
I am a student. my name is Chengang,age is 25
I am a student. my name is Ben,age is 18
I am a worker. my name is Hiong,age is 30
I am a worker. my name is Ben,age is 18
>Exit code: 0

转载地址:http://lnuwo.baihongyu.com/

你可能感兴趣的文章
人工智能,不止于技术的革命--WOT2017全球创新技术峰会开幕
查看>>
mysql 在大型应用中的架构演变
查看>>
ibm系列文章 --> Windows 到 Linux 之旅
查看>>
全备份失败后,如何手工清除exchange日志文件,附微软KB
查看>>
java如何连接mysq之源码l讲解
查看>>
企业运维笔试考题(1)
查看>>
Mysql修改存储过程相关权限问题
查看>>
4.2权限管理
查看>>
彻底理解ThreadLocal
查看>>
Node.js~ioredis处理耗时请求时连接数瀑增
查看>>
企业如何走出自己的CRM非常之道?
查看>>
整合看点: DellEMC的HCI市场如何来看?
查看>>
联合国隐私监督机构:大规模信息监控并非行之有效
查看>>
韩国研制出世界最薄光伏电池:厚度仅为人类头发直径百分之一
查看>>
惠普再“卖身”,软件业务卖给了这家鼻祖级公司
查看>>
软件定义存储的定制化怎么走?
查看>>
“上升”华为碰撞“下降”联想
查看>>
如何基于Spark进行用户画像?
查看>>
光伏发电对系统冲击大 “十三五”电力规划重点增强调峰能力
查看>>
全球19家值得关注的物联网安全初创企业
查看>>