ruby类方法ClassMethod&实例方法InstanceMethod小结

ClassMethod就是可以MyClass.method的类方法

InstanceMethod就是可以MyClass.new.method的实例方法

1.最正常在类里面定义的是

#定义 
class MyClass
	def self.class_method
		puts "self.class_method"
	end
	def instance_method
		puts "instance_method"
	end
end
#调用:
MyClass.class_method
MyClass.new.instance_method

2.更常用的是带module一起玩

module可以当成是一个组件,他扩展了类继承的功能。比如我们有Computer类和TV类,两个类都有Displayer,但问题他们的父类 ElectricalAppliance 却不是都有显示器的,这个时候他们都插入这个Displayer组件,很有意义。

也就是RUBY多继承的方法

module Mixin
  def foo
    puts 'foo'
  end
end
#调用,foo为类方法(3):
Class A
end
A.extend(Mixin)
class A 
  extend Mixin
end
class << A      #注意这里使用的是include
  include Mixin
end
#foo做为实例方法
Class A
end
A.include(Mixin)    # 这种写法在1.9.3中已经被删除,include变成私有变量,什么时候变的,不详
class A 
  include Mixin
end
class << a          #注意这个是对A实例a这个“对象”的,class << a 等价于class A
  include Mixin
end
以上其实很容易理解,组件的插入,extend则成为类方法,include则成为实例方法。
3. 在module中extend self

因为module本身也是一个class(ruby全家都是class),所以就得到一个Module的“类”方法,而在类中include或extend不影响

module Mixin
	extend self
	def foo
		puts "foo"
	end
end
#有如下
Mixin.foo
但是module是没有实例的,也就是不能Mixin.new ,所以你不能有
module Mixin  ; include Mixin ; end
事实如果我们这么做了,就会出现:ArgumentError: cyclic include detected ,重复的include操作。显然,我们不是不能,而是不需要。
4.module在module中,除了include或extend的时候需要Mixin::Methods之外没什么区别

5.self.included(base)

module Mixin
	def self.included(base)         #included是一个回调函数,当Mixin被include的时候被调用,base就是调用 w 
		base.extend(ClassMethods)
		base.send(:include, InstanceMethods)
	end
	module InstanceMethods
		def foo
		  puts "foo"
		end
	end
	module ClassMethods
		def bar
		  puts "bar"
		end
	end
end
class MyClass
	include Mixin
end
MyClass.bar
MyClass.new.foo

我们在看rails或别的代码的时候经常会看到self.included,他的作用是在别人include他的时候调用,这样就不用在class里一个一个的选择是include 还是extend了

 

转载于:https://my.oschina.net/zhao/blog/29243

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值