在 Eiffel 中实现迭代器 Class

Implementing Iterator Class in Eiffel

我从老师那里得到了这个测试 class,我在理解如何实现我的 class 以按此顺序创建自己时遇到问题。

代码:

 t5: BOOLEAN
    local
        bag: MY_BAG[STRING]
        bag3: like bag2
        bag4: MY_BAG[STRING]
    do
        comment("t5:test add_all, remove all, remove")
        bag := <<["nuts", 4], ["bolts", 11], ["hammers", 5]>> -- how to implement it to allow this?
        bag3 := <<["nuts", 2], ["bolts", 6], ["hammers", 5]>>
        -- add_all
        bag3.add_all (bag1)
        Result := bag3 |=| bag
        check Result end
        -- remove_all
        bag3.remove_all (bag1)
        Result := bag3 |=| bag2 and bag3.total = 13
        check Result end
        --remove
        bag4 := <<["nuts", 2], ["bolts", 6]>>
        bag3.remove ("hammers", 5)
        Result := bag3 |=| bag4
    end

    setup  -- inherit from ES_TEST redefine setup end
        -- this runs before every test
    do
        bag1 := <<["nuts", 2], ["bolts", 5]>>
        bag2 := <<["nuts", 2], ["bolts", 6], ["hammers", 5]>>
    end

目前,当我使用此类测试用例进行编译时,它会抛出编译器错误,提示创建不正确、不兼容

我的构造函数现在生成一个空的 HASH_TABLE,所以我的问题是如何确保我可以按照代码测试的方式初始化我的包 class?

我认为这个想法是使用转换而不是简单的创建,大致如下:

class MY_BAG [G]

create
    make

convert
    make ({ARRAY [TUPLE [G, INTEGER]]})

feature {NONE} -- Creation

    make (a: ARRAY [TUPLE [value: G; key: INTEGER]])
        do
            create storage.make (a.count)
            across
                a as c
            loop
                storage.extend (c.item.value, c.item.key)
            end
        end

feature {NONE} -- Storage

    storage: HASH_TABLE [G, INTEGER]

end

您可能还希望查看 class HASH_TABLE 中的注释子句,以了解 extend 是否适合您的情况或需要使用其他内容。