簡體   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