【问题标题】:Is an Interface considered a parent of a class that implements it?接口是否被视为实现它的类的父级?
【发布时间】:2012-08-27 21:25:39
【问题描述】:

我在这里有一个框架,它使用一个对象在其他对象与之交互时维护它们之间的状态。我希望与状态交互的对象(“Actions”)通常可以接受一种接口类型,State 对象将实现该接口,但我无法完成这项工作。

我想根据特定的状态实现(或接口)来定义框架对象本身,并且能够向其中添加操作,只要它们直接或通过该状态的接口接受定义的状态实现。

据我了解,<? super S> 定义的意思是“某种类型是 S 的父类”,但我不确定由 S 实现的接口是否符合此描述,因为它不完全是父类。

这是一个例子(不完全是编译代码):

Class Framework<State> {
    addAction(Action<? super State> a) {} // adds to a list of actions to be run

    run(State s) {
        for(Action<? super State> a : actions) {
            a.perform(s);
        }
    }
}

Interface IState1 {}

Interface IState2 {}

Interface Action<State> {
    public void perform(State s);
}

Class Action1 implements Action<IState1> {}

Class Action2 implements Action<IState2> {}

Class CombinedState implements IState1, IState2 {}

public static void run() {
    Framework<CombinedState> f = new Framework<CombinedState>();

    // add is expecting Action<? super CombinedState> but getting Action<IState1>
    f.add(new Action1()); // This doesn't seem to work
    f.add(new Action2());

    f.run(new CombinedState());
}

就泛型而言,接口是否算作父类?有什么方法可以实现不需要反射吗?

【问题讨论】:

  • 你想接受从 State 接口扩展而来的对象吗?
  • 我想接受能够直接使用CombinedState的Action,或者因为CombinedState实现了他们接受的接口。
  • 好吧,如果: 1) 你用小写首字母写了classinterface; 2)Framework 类有一个add() 方法(实际上有addAction()
  • 这段代码中的State是什么?这就像说T还是State是在某个地方定义的接口?请输入实际代码
  • 这就像说'T',但具有更具描述性的类型变量名称。 @Natix:原来这正是问题所在:)

标签: java generics inheritance multiple-inheritance


【解决方案1】:
import java.util.ArrayList;

class State{}

class Framework<State> 
{
    public ArrayList< Action< ? super State > >actions = new ArrayList< Action< ?super State > >();

    void add(Action<? super State> a) {

        actions.add(a);

    } 

    void doRun(State s) {
        for(Action<? super State> a : actions) {
            a.perform(s);
        }
    }
}

interface IState1 {}

interface IState2 {}

interface Action<State> {
    public void perform(State s);
}

class Action1 implements Action<IState1> {
    public void perform( IState1 s )
    {
        System.out.println("Do something 1");
    }
}

class Action2 implements Action<IState2> {

    public void perform( IState2 s )
    {
        System.out.println("Do something 2");
    }   
}

class CombinedState implements IState1, IState2 {}

class Untitled {
    public static void main(String[] args) 
    {
         Framework<CombinedState> f = new Framework<CombinedState>();

            // add is expecting Action<? super CombinedState> but getting Action<IState1>
            f.add(new Action1()); // This doesn't seem to work
            f.add(new Action2());

            f.doRun(new CombinedState());
    }
}

这在 OSX 上的 CodeRunner 中运行。

【讨论】:

  • 您将 State 定义为一个类。有必要吗?
  • @Bracket:我也认为没有必要。在Framework&lt;State&gt; 中,State 只是一个类型参数(与T 相同)。
  • 哦,我觉得自己很愚蠢。我在我的代码中使用“add”而不是“addAction”,这是我实际定义的。我现在因为一个简单的错别字而走到这一步,我感到很尴尬。但是看到它对你有用让我意识到我的错误!
猜你喜欢
  • 1970-01-01
  • 2015-01-15
  • 2016-06-20
  • 2018-10-24
  • 1970-01-01
  • 1970-01-01
  • 2016-09-30
  • 2013-06-08
  • 1970-01-01
相关资源
最近更新 更多