【问题标题】:Is it possible to make a create which returns a boolean是否可以创建一个返回布尔值的创建
【发布时间】:2020-05-19 18:08:55
【问题描述】:

你好,我不知道我问清楚了没有。

我有一个延迟类“动物”,它包含两个特征:“咬”(返回布尔值 -> 咬:BOOLEAN)和“说话”(说话(字:布尔))。

现在我创建了一个名为“dog”的类,它继承自“animal”。我重新定义了这两个功能而没有编译器错误。现在我想做一个创建,其中包含函数咬(创建咬:BOOLEAN)。

这给了我一个编译器错误,如果我尝试使用其他功能它可以正常工作。

我的错误代码:VGCP(2) 创建者部分列出了不正确的标识符。

谢谢你的帮助

我的应用程序.e:

note
    description : "root class of the application"
    date        : "$Date$"
    revision    : "$Revision$"

class
    APPLICATION

inherit
    ARGUMENTS_32

create
    make


feature
    d1:DOG

feature {NONE} -- Initialization

    make
            -- Run application.
        do
            print(d1.bite)
            print ("Hello Eiffel World!%N")
        end

end

我的动物班:

deferred class 
    ANIMAL

    feature 
        bite_:BOOLEAN
            deferred 
            end


    feature 
        speak(word:BOOLEAN)
            deferred 
            end 
end            

我的狗班:

class 
    DOG

inherit 
    ANIMAL

        redefine 
            bite_,speak_

                end

create 
    bite_

    feature 
        bite_:BOOLEAN
            do 
                Result:=5<3

            end



    feature 
        speak(word:BOOLEAN)
            do 


                print("yes")

            end 

【问题讨论】:

  • 请分享代码,以便我们确切知道您在说什么。
  • 谢谢,我添加了一些示例代码
  • 不清楚你为什么想让create返回BOOLEAN

标签: eiffel eiffel-studio-18


【解决方案1】:

我不确定功能speak 的预期语义是什么,所以我将只关注功能bite,或者更好地表达假设的意图can_biteANIMAL 类与你的类类似:

deferred class
    ANIMAL

feature -- Status report

    can_bite: BOOLEAN
            -- Can this animal bite?
        deferred 
        end

end

对于DOG 类,可能有两种变化:当每只狗咬人时(可能取决于某些内部状态),当在对象创建时设置咬人能力时。

在第一个场景中,can_bite 是一个函数,类看起来像

class
    DOG

inherit
    ANIMAL

feature -- Status report

    can_bite: BOOLEAN
            -- Can this animal bite?
        do
             Result := True -- It could also be some expression.
        end

end

在第二种情况下,can_bite 是一个由创建过程初始化的属性,类看起来像

class
    DOG

inherit
    ANIMAL

create
    make

feature {NONE} -- Creation

    make (is_biting: BOOLEAN)
            -- Initialize with ability `is_biting`.
        do
            can_bite := is_biting
        end

feature -- Status report

    can_bite: BOOLEAN
            -- Can this animal bite?

end

请注意,在这两种情况下,客户端都必须先创建 dog 对象,然后再对其进行调用。第一种情况,使用默认的创建过程,客户端代码如下

create d1
print (d1.can_bite)

第二种情况,使用了具体的创建过程(初始化can_bite),客户端代码如下

create d1.make (True)
print (d1.can_bite)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-07
    • 2015-11-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多