埃菲尔铁塔:一种检查给定CLASS_NAME类型符合性的方法

时间:2019-06-27 23:50:06

标签: standards-compliance eiffel

我正在尝试做某事

work (a_father: FATHER)
    do
        if a_father.conforms_to ({DEVELOPER}) then
           a_father.code
        else
           a_father.change_job ({DEVELOPER})
        end
    end

enter image description here enter image description here

编译有效,但是在我的实现@runtime中,它没有通过。我输错了什么?

2 个答案:

答案 0 :(得分:1)

示例中的问题是您试图查看类型FATHER(对象a_father的类型)是否符合类型TYPE [DEVELOPER](对象{ {1}}。

您应该做的是:

{DEVELOPER}

因此将if a_father.generating_type.is_conforming_to ({DEVELOPER}) then TYPE [FATHER]进行比较。

请注意,我认为通过将TYPE [DEVELOPER]替换为is_conforming_to可以起作用,但是类conforms_to为该例程TYPE引入了更具体的参数类型。

答案 1 :(得分:1)

我最好使用内置机制来检查对象类型的符合性:

if attached {DEVELOPER} a_father as dev then
     dev.code
else
     a_father.rest
end

并在前提条件中使用相同的方法:

attached {RELATED_DB_ENTITY} a_relationship_entity

对象测试可以完成您想做的事情:它检查附加到参数a_relationship_entity上的对象的类型是否符合类型RELATED_DB_ENTITY

相关问题