RUBY类特性

学习教材

RUBY类特性

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
        

输出:

>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.
>Exit code: 0

上一篇:hadoop day 3


下一篇:【POJ2242】The Circumference of the Circle(初等几何)