【问题标题】:Implementing Iterator Class in Eiffel在 Eiffel 中实现迭代器类
【发布时间】:2016-03-10 16:10:15
【问题描述】:

老师给了我这个测试课程,但我在理解如何实现我的课程以按此顺序创建自己时遇到问题。

代码:

 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,所以我的问题是如何确保我可以按照代码测试的方式初始化我的包类?

【问题讨论】:

    标签: eiffel


    【解决方案1】:

    我相信这个想法是使用转换而不是简单的创建,类似于这些思路:

    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
    

    您可能还希望查看HASH_TABLE 类中的注释子句,看看extend 是否适合您的情况或需要使用其他东西。

    【讨论】:

      猜你喜欢
      • 2016-03-30
      • 1970-01-01
      • 2016-10-24
      • 2018-03-07
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      相关资源
      最近更新 更多