翔如菲菲

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

导航

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   翔如飞飞  阅读(317)  评论(0编辑  收藏  举报

努力加载评论中...
点击右上角即可分享
微信分享提示