简体   繁体   English

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

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

I know how to fix it (see my solution @bottom) but don't understand why this compilation error occurs, as in my mind, renamed attributes should be created by the Precursor into default_create.我知道如何修复它(请参阅我的解决方案@bottom)但不明白为什么会发生此编译错误,因为在我看来,重命名的属性应该由 Precursor 创建到 default_create 中。 Why isn't that so?为什么不是这样?

NRJ_ENTITY 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_CONSUMER NRJ_消费者

inherit
    NRJ_ENTITY

end

NRJ_GENERATOR NRJ_GENERATOR

inherit
    NRJ_ENTITY

end

NRJ_GENERATOR_CONSUMER 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

end结尾

Error screenshot错误截图

在此处输入图片说明

Fix into NRJ_GENERATOR_CONSUMER修复到 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

Class NRJ_GENERATOR_CONSUMER has two versions of every attribute from NRJ_ENTITY .NRJ_GENERATOR_CONSUMER具有从每个属性的两个版本NRJ_ENTITY For example, current_day has versions current_day_generation and current_day_consumption .例如, current_daycurrent_day_generationcurrent_day_consumption版本。 The code in NRJ_ENTITY works only with one version of current_day , possibly renamed. NRJ_ENTITY的代码仅适用于current_day一个版本,可能已重命名。 It has no idea about the second version.它不知道第二个版本。 In order to tell which version of the replicated attribute (or a feature, in general) should be used, the class with the replication should select exactly one suitable version.为了确定应该使用哪个版本的复制属性(或一般特征),具有复制的类应该select一个合适的版本。

In the example, the selected version is current_day_generation .在示例中,所选版本为current_day_generation Therefore, default_create inherited from NRJ_ENTITY initializes it and not the other attribute.因此,从NRJ_ENTITY继承的default_create初始化它而不是其他属性。 In other words, with replication,换句话说,通过复制,

create current_day

is not automatically translated into不会自动翻译成

create current_day_generation
create current_day_consumption

but just into但只是进入

create current_day_generation -- The selected version.

This explains why you need the fix you are referring to.这解释了为什么您需要所指的修复程序。

Also, note that the instructions Precursor {NRJ_CONSUMER} and Precursor {NRJ_GENERATOR} call exactly the same version of default_create defined in NRJ_ENTITY , so one of the calls can be safely removed.另请注意,指令Precursor {NRJ_CONSUMER}Precursor {NRJ_GENERATOR}调用NRJ_ENTITY定义的default_create的完全相同版本,因此可以安全地删除其中一个调用。

Summary: Inherited code deals only with selected versions of replicated features.总结:继承的代码只处理复制功能的选定版本。

Corollary: Non-selected versions of replicated attributes have to be explicitly initialized in the class where they are replicated.推论:复制属性的非选择版本必须在复制它们的类中显式初始化。

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

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