#Class describ ------rdoc rules
#
= headers
#
this is header content
#
== sub header
#
this is sub header content
#
=== sub sub header
#
this is sub sub header
class Animal

attr_accessor :name
#-------------①

#this is the comment for the 'new' method !

def initialize type
name
= type #-------------②
end

#this is comment for 'shout' method
# Second Line
#* this is sub line 1
#* this is sub line 2

def shout
if name == 'dog'
puts
"wang! wang!"
elsif name
== 'cat'
puts
"miao! miao!"
elsif name
==nil
puts
"nil"
else
puts
"kao!"
end
end

end

dog
= Animal.new "dog"
puts dog.name
dog.shout
dog.name
= "cat"
dog.shout

本程序输出为:

nil

miao! miao!

-----------------

①: 成员变量

②: 临时变量

attr_accessor 表示后面的为类成员,同时为其提供外部访问能力。即相当于为其提供 get;  set;

相关文章:

  • 2022-02-05
  • 2021-12-29
  • 2021-07-29
  • 2022-01-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-19
猜你喜欢
  • 2022-01-21
  • 2022-01-11
  • 2022-01-16
  • 2021-12-16
  • 2021-12-14
  • 2021-08-30
  • 2021-05-25
相关资源
相似解决方案