代码之家  ›  专栏  ›  技术社区  ›  Pipo

Eiffel:从类继承并将当前对象转换为类的正确方法

  •  0
  • Pipo  · 技术社区  · 7 年前

    这不是我第一次尝试从类继承并将对象转换为类,在本例中是扩展 HTTP_CLIENT_RESPONSE 具有 valid_http_response => response.status = 200 valid_http_json_body => validate json content

    为此,我想继承他的遗产 和添加功能并创建我的自定义 DB_ENTITY_HTTP_CLIENT_RESPONSE 从 HTTP_客户端_响应

    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
    

    • 父级创建过程
    • deep_copy

    下面是我班上的其他同学:

    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
    

    正常地

    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
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Alexander Kogtenkov    7 年前

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