#############################################
#create ruby a class
#@符号表示实例变量,相当于java的private 属性
#############################################
class Person
def initialize(name,age)
@name =name
@age =age
end
end jake = Person.new("jake",18)
#inspect 方法可以发送给任何对象
puts jake.inspect
puts jake.to_s #为类添加新的属性
class Person
def to_s
"Person name=#{@name}-age=#{@age}"
end
end she = Person.new("she",18)
puts she.to_s#Person name=she-age=18 ###############################
#继承
###############################
class ChinaPerson <Person
def initialize(name,age,color)
super(name,age)#使用父类方法
@color =color
end
end huNanShe = ChinaPerson.new("hunanshe",20,"yellow")
puts huNanShe.to_s#Person name=hunanshe-age=20
05-11 19:38