0

我学习埃菲尔,我知道C我可以做这样的事情来设置头文件,例如::

#define USER_ACTIVE     0
#define WHEN_SOMETHING  1
#define WHERE_HAND      2
#define WHERE_ACTIVE    3
#define WHERE_GOOD      4

并且还有实例,例如:

typedef struct something {
   int user;
   int where;
   int somethingelse
}something;

甚至是集合函数指针:

typedef struct decisions {
  void (*init)(struct something *s, int who, double factor);
}decisions;

几乎所有普通的编程语言都是一样的故事。我一直在这里寻找 fx,除了它没有很好地翻译,对我来说很难掌握如何去做。所以有没有“正常”的方式来做这个用这种语言?还是所有都必须做意大利面条式?

谢谢

4

1 回答 1

1

这个例子可以翻译成这样的:

class MY_CONSTANTS feature
    user_active: INTEGER = 0
    when_something: INTEGER = 1
    where_hand: INTEGER = 2
    where_active: INTEGER = 3
    where_good: INTEGER = 4
end

class SOMETHING
feature -- Access
    user: INTEGER
    where: INTEGER
    somethingelse: INTEGER
feature -- Modification
    set_user (u: like user) do user := u end
    set_where (w: like where) do where := w end
    set_somethingelse (s: like somethingelse) do somethingelse := s end
end

deferred class DECISION feature
    init (s: SOMETHING; who: INTEGER; factor: REAL_64) deferred end
end

class DECISION_1 inherit DECISION feature
    init (s: SOMETHING; who: INTEGER; factor: REAL_64)
        do
            ...
        end
end

class DECISION_2 inherit DECISION feature
    init (s: SOMETHING; who: INTEGER; factor: REAL_64)
        do
            ...
        end
end

class MY_CLASS inherit
    MY_CONSTANTS
... -- user_active, when_something, etc. can be used here
feature
    something: SOMETHING
    decision: DECISION
...
           -- Somewhere in the code
        create something
        if ... then
            create {DECISION_1} decision
        else
            create {DECISION_2} decision
        end
        ...
        decision.init (something, ..., ...)
    ...
end
于 2013-10-28T10:25:39.320 回答