gpt4 book ai didi

ruby - Ruby 中的 `super` 是什么?

转载 作者:行者123 更新时间:2023-12-03 15:46:30 25 4
gpt4 key购买 nike

这个问题在这里已经有了答案:





Super keyword in Ruby

(6 个回答)


6年前关闭。




在网上浏览关于 ruby​​ on rails 的内容时,我看到了 super 这个词。 .有人能说出它是什么以及它能做什么吗?

最佳答案

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

如果还不够,可以从 here进一步学习。

关于ruby - Ruby 中的 `super` 是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31019009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com