翔如菲菲

其实天很蓝,阴云总会散;其实海不宽,此岸连彼岸.

导航

Ruby类变量和类方法

#类变量和类方法;类变量,类方法在 Java/C# 里与之相对应的是 static变量,static方法
class Student
  @@count=0
  def initialize
    @@count+=1
  end
  #定义类方法要在方法名前加上类名和一个点号“.”
  def Student.student_count
    puts "This class have #@@count students."
  end
end

p1=Student.new
p2=Student.new
Student.student_count # This class have 2 students.

p3=Student.new
p4=Student.new
Student.student_count # This class have 4 students.

posted on 2012-01-31 14:22  翔如飞飞  阅读(326)  评论(0)    收藏  举报