0
請注意,我的問題與this question不同,因爲我要求一個類的所有後代(包括後代的後代)。如何使用`self.inherited`在Ruby中獲取所有類的後代?
此外,我更願意使用像
class Animal
def self.inherited(subclass)
@descendants = []
@descendants << subclass
end
def self.descendants
puts @descendants
end
end
因爲它比讓所有的類和過濾的後代的方式更快。
懶實例化'@descendants || = []'和你做。儘管如此,這對後代的後代來說並不適用。答案相關_對整棵樹(後裔的後代)構成了詭計。 – mudasobwa