#类变量和类方法;类变量,类方法在 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.
@@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.