ruby入门_属性

class Person
  def initialize name,age,sex
    @name = name
    @age = age
    @sex = sex
  end
  # read
  attr_reader :name
  # write
  attr_writer :age
  # read and write
  attr_accessor :sex
end

tom = Person.new "Tom",20,"male"
puts tom.name
tom.age = 20
tom.sex = "female"
puts tom.sex

你可能感兴趣的:(Class,Ruby,accessor)