一、实例方法和类方法
class Person
def my_instance_method 实例方法
puts "I'm an instance method"
end
def self.my_class_method 类方法
puts "I'm a class method"
end
end
二、定义类方法多种方法
![](https://img.haomeiwen.com/i8144069/d866bc155357808c.png)
备注:第一种用类Person代替了self,第二中用class << self ,end来包含多个类方法
三、例子
![](https://img.haomeiwen.com/i8144069/f0158d7c2dd00127.png)
类方法:
![](https://img.haomeiwen.com/i8144069/4bee6f07dc4d100b.png)
实例方法:
![](https://img.haomeiwen.com/i8144069/a8b77583f8aa76b9.png)
一、实例方法和类方法
class Person
def my_instance_method 实例方法
puts "I'm an instance method"
end
def self.my_class_method 类方法
puts "I'm a class method"
end
end
二、定义类方法多种方法
备注:第一种用类Person代替了self,第二中用class << self ,end来包含多个类方法
三、例子
类方法:
实例方法:
本文标题:四、实例方法和类方法
本文链接:https://www.haomeiwen.com/subject/rzusuxtx.html
网友评论