ruby 笔记之三

puts "nihao,ruby!" ;

=begin
    Object-oriented programming
    family first
=end

1.
#so far,Object-oritend programming Class study



=begin
   1 task
     new create user fzdc
     ok
   2 task 
      数字转字符串
      2.to_s
      字符串转数字
      "1".to_i      
=end

print "/n" ;

def add(a,b=1)
  print a + b ;
end
add(1,5) ;
print "/n" ;

puts "1".to_i + 2 ;

 

2.

 # define a Class Person

class Person
 
   def initialize(name,age=18)
      @name = name ;
      @age = age ;
      @motherland = "China" ;
    end #initialize end
   
    def talk
       puts "my name is/t" +@name+",age is "+@age.to_s  ;
       if @motherland == "China" then
            puts "i am chinese" ;
       else
            puts "i am foreigner" ;
      end
         
    end # talk end
    attr_writer :motherland

end #Person end

p1 = Person.new("samba",24) ;
p1.talk ;

p2 = Person.new("gjhohj",24) ;
p2.motherland = "USA" ;

p2.talk ;

#attr_writer :motherland 相当于 set method
#attr_reader :motherland 相当于 get method

class Student < Person
  def talk
    puts "i am very happy!" ;
  end
 
end

p = Student.new("nihao",10) ;
p.talk ;

3.

# test extends

class Student
     def initialize(a,age=10)
       puts a,"/n" ;
     end
    
     def talk
       puts "nihao extends" ;
     end
    
end
  

  p = Student.new("a") ;
  p.talk ;
  
  
   def sum(a)
     if a==2
      puts a
     return a
     end
   end    
  
   print sum(2)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值