Ruby继承链
文章目录
module M1
end
module M2
end
module M3
end
module M4
end
class Hello
include M1
include M2
# This is the position of class self
prepend M3
prepend M4
end
p Hello.ancestors
# M4, M3, Hello, M2, M1, Object, Kernel, BasicObject
列出类继承链的技巧:
将
include的模块按顺序排在上面部分将
prepend的模块按顺序排在下面部分将类本身放在
include和prepend的中间位置继承顺序就是从下往上数,包括类本身

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论