实现一个接口,该接口具有返回接口的方法但出现未实现的错误
Implementing an interface that has a method returning an interface but getting a not implemented error
我正在为 AI 做一些事情 class 但我似乎有一些项目结构问题。我知道这在 Python 中更容易完成,但我已经这样做了,我在 Java 中这样做是因为我有时间消磨时光。
我的 classes 设置如下:
- 我有 3 个接口:
ProblemInterface
、ActionInterface
、StateInterface
。
- 我有一个搜索 class,它调用
ProblemInterface
中定义的方法,因此保持 ProblemInterface
的 ArrayList
。
- 我有一个
ProblemQ1
class(试图)实现 ProblemInterface
。其他 classes ActionQ1
和 StateQ1
实现了各自的接口。
class ProblemQ1 implements ProblemInterface
class ActionQ1 implements ActionInterface
class StateQ1 implements StateInterface
ProblemInterface
定义了一个名为 actions()
的方法,它接受一个 StateInterface
参数和 return 一个 ArrayList<ActionInterface>
。所以,像这样:
ArrayList<ActionInterface> actions(StateInterface state);
- 因此,我的
ProblemQ1
class 试图定义 actions()
,方法是让它接受一个 StateQ1
参数并将它 return 一个 ArrayList
的 ActionQ1
个。
public ArrayList<ActionQ1> actions(StateQ1 state)
ProblemQ1
class不能是抽象的,因为我必须为我想解决的每个问题实例化它。
- 稍后我必须制作其他
ProblemQN
、StateQN
和 ActionQN
classes,它们实现了各自的接口。
但是,我收到一个编译器错误,告诉我我的 ProblemQ1
class 必须是抽象的或实现 ProblemInterface
方法。从我所看到的,它遵循接口的规则,我不明白为什么它不把在 ProblemQ1
中定义 actions()
视为定义接口。
From what I'm seeing, it follows the rules of the interface, and I
can't see why it wouldn't treat defining actions() in ProblemQ1 as
defining the interface.
没有你在ProblemQ1
中更改了方法的参数类型:
public ArrayList<ActionQ1> actions(StateQ1 state)
虽然接口定义了这样的方法:
ArrayList<ActionInterface> actions(StateInterface state);
这意味着您重载而不是重写该方法。
用 @Override
注释该方法,您会看到编译器不会将其视为覆盖。
另请注意,覆盖的 return 类型也不兼容。
ArrayList<ActionQ1>
不是 ArrayList<ActionInterface>
的子类。
但是 ArrayList<ActionQ1>
是 ArrayList<? extends ActionInterface>
的子类。
要解决您的问题,您可以在接口中引入泛型:
public interface ProblemInterface<T extends StateInterface, U extends ActionInterface>{
ArrayList<U> actions(T state);
}
实现可能是这样的:
public class ProblemQ1 implements ProblemInterface<StateQ1, ActionQ1>{
public ArrayList<ActionQ1> actions(StateQ1 state){
// ...
}
}
我正在为 AI 做一些事情 class 但我似乎有一些项目结构问题。我知道这在 Python 中更容易完成,但我已经这样做了,我在 Java 中这样做是因为我有时间消磨时光。
我的 classes 设置如下:
- 我有 3 个接口:
ProblemInterface
、ActionInterface
、StateInterface
。 - 我有一个搜索 class,它调用
ProblemInterface
中定义的方法,因此保持ProblemInterface
的ArrayList
。 - 我有一个
ProblemQ1
class(试图)实现ProblemInterface
。其他 classesActionQ1
和StateQ1
实现了各自的接口。class ProblemQ1 implements ProblemInterface
class ActionQ1 implements ActionInterface
class StateQ1 implements StateInterface
ProblemInterface
定义了一个名为actions()
的方法,它接受一个StateInterface
参数和 return 一个ArrayList<ActionInterface>
。所以,像这样:ArrayList<ActionInterface> actions(StateInterface state);
- 因此,我的
ProblemQ1
class 试图定义actions()
,方法是让它接受一个StateQ1
参数并将它 return 一个ArrayList
的ActionQ1
个。public ArrayList<ActionQ1> actions(StateQ1 state)
ProblemQ1
class不能是抽象的,因为我必须为我想解决的每个问题实例化它。- 稍后我必须制作其他
ProblemQN
、StateQN
和ActionQN
classes,它们实现了各自的接口。
但是,我收到一个编译器错误,告诉我我的 ProblemQ1
class 必须是抽象的或实现 ProblemInterface
方法。从我所看到的,它遵循接口的规则,我不明白为什么它不把在 ProblemQ1
中定义 actions()
视为定义接口。
From what I'm seeing, it follows the rules of the interface, and I can't see why it wouldn't treat defining actions() in ProblemQ1 as defining the interface.
没有你在ProblemQ1
中更改了方法的参数类型:
public ArrayList<ActionQ1> actions(StateQ1 state)
虽然接口定义了这样的方法:
ArrayList<ActionInterface> actions(StateInterface state);
这意味着您重载而不是重写该方法。
用 @Override
注释该方法,您会看到编译器不会将其视为覆盖。
另请注意,覆盖的 return 类型也不兼容。
ArrayList<ActionQ1>
不是 ArrayList<ActionInterface>
的子类。
但是 ArrayList<ActionQ1>
是 ArrayList<? extends ActionInterface>
的子类。
要解决您的问题,您可以在接口中引入泛型:
public interface ProblemInterface<T extends StateInterface, U extends ActionInterface>{
ArrayList<U> actions(T state);
}
实现可能是这样的:
public class ProblemQ1 implements ProblemInterface<StateQ1, ActionQ1>{
public ArrayList<ActionQ1> actions(StateQ1 state){
// ...
}
}