在Ruby中,有模块,您可以通过“混合”模块来扩展一个类。 module MyModule
def printone
print "one"
end
end
class MyClass
include MyModule
end
theOne = MyClass.new
theOne.printone
>> one
在Objective-C
我试图做到以下几点: class A {
def foo() { "foo" }
}
class B {
def bar() { "bar" }
}
A.mixin B
def a = new A()
a.foo() + a.bar()
一个显著差异 - 我想这样做实例上的mixin: a.mixin B
但这导致 groovy.lang.Missin
好奇如何从一个活动记录类包含的模块的实例方法中调用一个类方法。例如,我希望用户和客户端模型共享密码加密的细节和螺栓。 # app/models
class User < ActiveRecord::Base
include Encrypt
end
class Client < ActiveRecord::Base
include Encrypt
end
# app/