【问题标题】:How to make methods in one class accessible from another one?如何使一个类中的方法可以从另一个类中访问?
【发布时间】:2015-04-23 11:01:40
【问题描述】:

我是埃菲尔的初学者。我有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 类访问和调用?

【问题讨论】:

    标签: function methods call eiffel


    【解决方案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”关系。例如,一个苹果有一种颜色,但它不是一种颜色,因此客户-供应商关系更适合。另一方面,它是果实,所以继承关系更适合。在后一种情况下,苹果类通过指定一些其他属性(如形状和种子位置)来改进水果类。但它不能细化颜色类。与您的示例相同:它看起来不像 APPLICATIONBLUE 的示例,因此客户-供应商关系似乎更适合。

    【讨论】:

      【解决方案2】:

      APPLICATION类中,可以添加BLUE类型的局部变量b,然后调用make作为其创建过程:

      make
          local
              b: BLUE
          do
              create b.make
          end
      

      【讨论】:

        【解决方案3】:

        首先,在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
        

        【讨论】:

          猜你喜欢
          • 2017-01-03
          • 2018-10-18
          • 1970-01-01
          • 1970-01-01
          • 2022-12-05
          • 1970-01-01
          • 1970-01-01
          • 2013-03-07
          • 2012-05-18
          相关资源
          最近更新 更多