0

我是埃菲尔的初学者。我有2节课。主要的称为APPLICATION

class
    APPLICATION

inherit
    ARGUMENTS

create
    make

feature {NONE} -- Initialization

    make
            -- Run application.
        do
            print ("test")
        end    
end

和另一个名为BLUE

class
    BLUE
create
    make

feature
    make
        local
            dead:BOOLEAN
            active:BOOLEAN
            number:BOOLEAN

        do
            io.putstring ("writetest")
        end

end

我想知道如何使类中的方法BLUE可以从类中访问和调用APPLICATION

4

3 回答 3

1

在 classAPPLICATION中,您可以添加一个局部变量btype BLUE,然后调用make它作为其创建过程:

make
    local
        b: BLUE
    do
        create b.make
    end
于 2015-02-22T19:45:35.070 回答
1

一般来说,面向对象语言中的类之间有两种关系,允许一个类访问另一个类的特性:

  1. 继承,和
  2. 客户-供应商关系。

在第一种情况下,类继承了父类的所有特性,并且可以将它们称为自己的:

class APPLICATION

inherit
    BLUE
        rename
                -- Class `APPLICATION' has `make' as well,
                -- so the inherited one has to be renamed to avoid a clash.
            make as make_blue
        end

create
    make

feature {NONE} -- Initialization

    make
            -- Run application.
        do
                -- This prints "test".
            print ("test")
                -- Call the inherited feature `{BLUE}.test'
                -- renamed above into `make_blue'
                -- that prints "writetest".
            make_blue
        end

end

在继承的情况下,调用是在同一个对象上执行的。

在客户-供应商关系中,调用是在不同的对象上执行的。在您的示例中,要调用的功能与创建过程一致,因此该调用成为正在创建的对象的创建指令的一部分:

class APPLICATION

create
    make

feature {NONE} -- Initialization

    make
            -- Run application.
        local
            other: BLUE
        do
                -- This prints "test".
            print ("test")
                -- Create an object of type `BLUE'
                -- calling its creation procedure `make'
                -- that prints "writetest".
            create other.make
        end

end

关于何时使用一种方法而不是另一种方法的粗略近似如下。我们可以将继承视为“is-a”,将客户-供应商视为“has”关系。例如,一个苹果有一种颜色,但它不是一种颜色,因此客户-供应商关系更适合。另一方面,它是果实,所以继承关系更适合。在后一种情况下,苹果类通过指定一些其他属性(如形状和种子位置)来改进水果类。但它不能细化颜色类。与您的示例相同:它看起来不像APPLICATION是 的示例BLUE,因此客户-供应商关系似乎更合适。

于 2015-02-23T07:36:48.013 回答
1

首先,在BLUE你需要一个方法的时候,你不应该在create方法里写,这会让你写程序更加困难。特别是随着程序变得越来越复杂。所以我添加了write_message它不是一个创建方法。

class
    BLUE

feature
    write_message
        local
            dead:BOOLEAN
            active:BOOLEAN
            number:BOOLEAN
        do
            io.putstring ("writetest")
        end
end

现在,我们需要调用新方法

class
    APPLICATION   
inherit
    ARGUMENTS
create
    make

feature {NONE} -- Initialization

    make
            -- Run application.
        local
             blue: BLUE
        do
            print ("test")
            create blue
            blue.write_message
        end
end
于 2015-07-25T21:27:14.257 回答