Eiffel:从 class 继承并将 Current 对象转换为它的正确方法

Eiffel: a proper way to inherit from a class and convert Current object to it

这不是我第一次尝试从 class 继承并将对象转换为它,在这种情况下,使用 valid_http_response => response.status = 200、[= 扩展 HTTP_CLIENT_RESPONSE 的功能16=], 等等...

为此,我想继承 HTTP_CLIENT_RESPONSE 并添加功能并从 HTTP_CLIENT_RESPONSE

创建我的自定义 DB_ENTITY_HTTP_CLIENT_RESPONSE
test_case
    local
        l_http_client_r: HTTP_CLIENT_RESPONSE
        l_db_entity_http_client_r: DB_ENTITY_HTTP_CLIENT_RESPONSE
    do
        l_http_client_r := execute_get("someURL") -- returns an HTTP_CLIENT_RESPONSE
        l_db_entity_http_client_r := l_http_client_r
        assert("valid response", l_db_entity_http_client_r.valid_response)
    end

我似乎很难设置内部属性...最好的方法是什么?我有同样的情况试图创建一个 WATT class 继承自 NATURAL_32 这是一个扩展。

在我的策略中,我尝试调用创建者

这是我 class 尝试的其余部分:

class
    DB_ENTITY_HTTP_CLIENT_RESPONSE

inherit
    HTTP_CLIENT_RESPONSE

create
    make_from_http_client_response

convert
    make_from_http_client_response ({HTTP_CLIENT_RESPONSE})

feature -- Initialization

    make_from_http_client_response (a_client_response: HTTP_CLIENT_RESPONSE)
        do
            make (a_client_response.url)
            deep_copy (a_client_response)
        end

feature -- Status report

    valid_response: BOOLEAN
        do
            Result := status = 200
        end

我发现目前唯一可行的方法是将所有属性设置为其他,这通常是 deep_copy 的语义...

make_from_http_client_response (a_client_response: HTTP_CLIENT_RESPONSE)
    do
        make (a_client_response.url)
        set_body (a_client_response.body)
        set_http_version (a_client_response.http_version)
        set_error_occurred (a_client_response.error_occurred)
        set_error_message (a_client_response.error_message)
        set_raw_header (a_client_response.raw_header)
        set_status_line (a_client_response.status_line)
        ... I surely forgot something...
    end

没有内置功能可以从另一种类型的对象初始化一种类型的对象。特征 copydeep_copy 需要相同类型的对象。因此,在代码中显式设置属性是可行的方法。

另一种选择是采用客户-供应商关系而不是继承。这是否合适,取决于应用程序。