繁体   English   中英

重命名为继承人后变量设置不正确

[英]variable is not properly set after renaming into heir

我知道如何修复它(请参阅我的解决方案@bottom)但不明白为什么会发生此编译错误,因为在我看来,重命名的属性应该由 Precursor 创建到 default_create 中。 为什么不是这样?

NRJ_ENTITY

inherit
    ANY
        redefine
            default_create
        end

feature {NONE} -- Initialization

    default_create
        do
            create current_day
            create current_month
            create current_year
            Precursor
        end

feature -- Access

    current_day,
    current_month,
    current_year: ENERGY_UNIT

end

NRJ_消费者

inherit
    NRJ_ENTITY

end

NRJ_GENERATOR

inherit
    NRJ_ENTITY

end

NRJ_GENERATOR_CONSUMER

inherit
    NRJ_GENERATOR
        rename
            current_day as current_day_generation,
            current_month as current_month_generation,
            current_year as current_year_generation
        redefine
            default_create
        select
            current_day_generation,
            current_month_generation,
            current_year_generation
        end
    NRJ_CONSUMER
        rename
            current_day as current_day_consumption,
            current_month as current_month_consumption,
            current_year as current_year_consumption
        redefine
            default_create
        end

feature {NONE} -- Initialize

    default_create
        do
            Precursor {NRJ_GENERATOR}
            Precursor {NRJ_CONSUMER}
        end

结尾

错误截图

在此处输入图片说明

修复到 NRJ_GENERATOR_CONSUMER

default_create
    do
        create current_day_consumption
        create current_month_consumption
        create current_year_consumption
        Precursor {NRJ_CONSUMER}
        Precursor {NRJ_GENERATOR}
    end

NRJ_GENERATOR_CONSUMER具有从每个属性的两个版本NRJ_ENTITY 例如, current_daycurrent_day_generationcurrent_day_consumption版本。 NRJ_ENTITY的代码仅适用于current_day一个版本,可能已重命名。 它不知道第二个版本。 为了确定应该使用哪个版本的复制属性(或一般特征),具有复制的类应该select一个合适的版本。

在示例中,所选版本为current_day_generation 因此,从NRJ_ENTITY继承的default_create初始化它而不是其他属性。 换句话说,通过复制,

create current_day

不会自动翻译成

create current_day_generation
create current_day_consumption

但只是进入

create current_day_generation -- The selected version.

这解释了为什么您需要所指的修复程序。

另请注意,指令Precursor {NRJ_CONSUMER}Precursor {NRJ_GENERATOR}调用NRJ_ENTITY定义的default_create的完全相同版本,因此可以安全地删除其中一个调用。

总结:继承的代码只处理复制功能的选定版本。

推论:复制属性的非选择版本必须在复制它们的类中显式初始化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM