在 Internet 上浏览有关 ruby on rails 的信息时,我看到了super. 有人能说出它是什么以及它能做什么吗?
G.B*_*G.B 13
super 方法调用父类方法。
例如:
class A
def a
# do stuff for A
end
end
class B < A
def a
# do some stuff specific to B
super
# or use super() if you don't want super to pass on any args that method a might have had
# super/super() can also be called first
# it should be noted that some design patterns call for avoiding this construct
# as it creates a tight coupling between the classes. If you control both
# classes, it's not as big a deal, but if the superclass is outside your control
# it could change, w/o you knowing. This is pretty much composition vs inheritance
end
end
Run Code Online (Sandbox Code Playgroud)
如果这还不够,那么你可以从这里进一步学习