首页 > 解决方案 > 如何在多继承中合并属性

问题描述

重复从具有相同父级的2个类继承,我陷入了继承2次相同属性的经典案例。我想将 2 个属性合并为一个,并尝试使用 undefine 来完成,但它给我一个编译错误。我看到的另一个解决方案是重命名父母双方之一的属性,但据我所知,我的 D 类的每个实例都有一个无用的属性,这不是我想要的......

Error: Undefine subclause lists name of frozen feature or attribute or C external. What to do: unless you can change the status of the feature in the parent, remove its name from Undefine subclause since it cannot be undefined.

如何从重复继承的类中合并 2 个属性

class A
    serial: STRING

end -- class A

class B

inherit
    A

end -- class B


class C

inherit
    A

end -- class C


class D

inherit
    B
        undefine 
            serial -- error seems to appear here in that case
        end
    C

end -- class D

标签: multiple-inheritanceeiffel

解决方案


没有理由取消定义将与来自不同继承路径的相同版本合并的功能。在示例中,属性serial未在BC和中更改D。因此,继承BC不进行任何改编都是可以的:

class D inherit
    B
    C
end

推荐阅读