在Eiffel中比较对象/值而不是引用

时间:2016-01-25 23:53:26

标签: comparison eiffel

我一直试图弄清楚如何使后置条件适用于以下代码。 有3个类,Bank是Customer的客户,Customer是Account

的客户

这是银行类,我只是不能通过后置条件other_customer_unchanged

    new (name1: STRING)
        -- Add a new customer named 'name1'
        -- to the end of list `customers'
    require
        customer_not_already_present:customer_exists(name1)=false

    do
        customers.force (create {CUSTOMER}.make (name1))
        count := customers.count

    ensure
        total_balance_unchanged:
            sum_of_all_balances = old sum_of_all_balances
        num_customers_increased:count /= old count and old count+1=count
        total_unchanged:total = old total
        customer_added_to_list:
            customer_exists (name1)
            and then customers[customer_id (name1)].name ~ name1
            and then customers[customer_id (name1)].balance ~ zero
        other_customers_unchanged:
            customers_unchanged_other_than(name1, old customers.deep_twin)
    end

以下是customers_unchanged_other_than

的功能
customers_unchanged_other_than (a_name: STRING;old_customers:like customers): BOOLEAN
        -- Are customers other than `a_name' unchanged?
    local
        c_name: STRING
    do
        from
            Result := true
            customers.start
        until
            customers.after or not Result
        loop
            c_name := customers.item.name
            if c_name /~ a_name then
                Result := Result and then
                    old_customers.has (customers.item)
            end
            customers.forth
        end
    ensure
        Result =
            across
                customers as c
            all
                c.item.name /~ a_name IMPLIES
                    old_customers.has (c.item)
            end
    end

我已重新定义了客户类

中的is_equal功能
is_equal (other: like Current): BOOLEAN
    do
        Result := name ~ other.name and balance = other.balance
    ensure then
        Result = (name ~ other.name and balance = other.balance)
    end

我已经查看了旧的customer.deep_twin中的内容,它确实包含了客户的项目,但不知何故,当它使用.has功能时,它只会使结果为false。 非常感谢任何帮助:)

1 个答案:

答案 0 :(得分:1)

我从您的代码中假设客户和old_customers属于CONTAINER的类型后代(ARRAY,LIST,STACK,QUEUE等)。然后您可以使用customers.compare_objects(或old_customers.compare_objects)来请求CONTAINER使用is_equal搜索时代替“=”。