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.

 

学习教材

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

 

目录
相关文章
|
4月前
|
Ruby
|
4月前
|
Ruby
|
4月前
|
Ruby
|
4月前
|
Ruby
|
5月前
|
Ruby
|
5月前
|
Ruby
|
5月前
|
Ruby
|
5月前
|
Ruby
|
Java 程序员 数据安全/隐私保护
【Ruby高级技术】对面向对象里的控制访问,包括类的继承类常量的深入理解和使用
【Ruby高级技术】对面向对象里的控制访问,包括类的继承类常量的深入理解和使用
|
存储 Ruby
Ruby Programming | 连载 10 - Ruby 的类
Ruby Programming | 连载 10 - Ruby 的类