接口中的抽象类返回 [英] Abstract class return in interface
问题描述
首先,这是Java中的新功能,很抱歉,如果这是一个简单的问题。
First of all, pretty new in java, sorry if it is a simple question.
我实现了一个抽象类,并扩展了两个类。
I have implemented an abstract class, and two classes that extends it.
public abstract class Lado{
//body
}
以及其他两个具有这种形式的类
and the two other classes with this form for example
public class Arista extends Lado
{ //body
}
因此,没有问题,现在我已经定义了这样的接口。
so, no problem there, now i have an interface defined like this.
public interface Grafo
{ //body
public List<Lado> lados();
//more body
}
您是否看到界面返回列表Lado类,
,但在我的实现中,我需要返回Arista类或第二类的列表(在接口的另一个实现上)
has you see the interface returns a List of class Lado, but in my implementation I need to return a List of class Arista or the second class (on another implementation of the interface)
我用于接口实现的代码库具有lados()的实现
in the codebase that I HAVE to use for the interface implementation have the implementation of lados() like this
public List<Lado> lados() {
return lados;
}
在我的代码中定义了lados,例如
with lados defined in my code like
private List<Arista> lados;
并已初始化
lados = new LinkedList<Arista>();
现在当然返回错误了
GrafoNoDirigido.java:141: error: incompatible types
return lados;
^
required: List<Lado>
found: List<Arista>
1 error
我似乎无法找到如何在不修改基本代码的情况下解决此问题的方法被给了我(我重复不能改变)。我知道我无法实例化一个抽象类对象,并且抽象类实现具有抽象类所没有的值和功能。
I cant seem to find how to fix this without modifying the base code that was given to me (and I repeat cant change it). I know I cant instantiate an abstract class object and the abstract class implementation has values and functions that the abstract class doesn't.
出于相同的原因,我也不能使用重写我无法在实现中更改函数签名。
Also I cant use override for the same reason that i cant change my functions signatures in my implementations.
谢谢您的帮助,谢谢。
如果有什么奇怪的内容,请告诉我。
If anything reads weird, please let me know.
--------------------编辑---- -----
Lado抽象类。
-------------------- EDIT --------- Lado abstract class.
public abstract class Lado
{
private String id;
private double peso;
public Lado(String id, double peso) {
this.id = id;
this.peso = peso;
}
public String getId() {
return id;
}
public double getPeso() {
return peso;
}
public abstract String toString();
}
Arista子类
public class Arista extends Lado
{
private Vertice u;
private Vertice v;
public Arista(String id, double peso, Vertice u, Vertice v){
super(id , peso);
this.u = u;
this.v = v;
}
public Vertice getExtremo1() {
return u;
}
public Vertice getExtremo2() {
return v;
}
public String toString() {
String s = "string";
return s;
}
}
然后,如果我返回 List< Lado>
如果您尝试执行 getExtremo1()
,则确实会在编译时找到该符号(Lado类上不存在),尝试将输出列表转换为 List< Arista>
,但它也无法正常工作。
Then if I return a List<Lado>
if y try to do getExtremo1()
it doenst find the symbol on compilation (no existant on the Lado class), tried to cast the output list like a List<Arista>
but it didnt worked correctly either.
推荐答案
尝试将您的界面更改为:
Try changing your interface to:
公共列表lados();
public List<? extends Lado> lados();
-或-
您可以走另一条路线,并使列表成为带有子实例的父类型:
You can go another route and have the list be of the parent type with child instances:
private List<Lado> lados;
@Override
public List<Lado> lados() {
// Type of lados is List<Lado>, but we will fill it up with Arista instances...
lados = new LinkedList<Lados>();
lados.add(new Arista());
// if Arista has methods that follow the with setter stereotype, ie: public Arista withId(int id) { this.setId(id); return this; } you can in-line create them:
lados.add(new Arista()
.withId(id)
.withName(name)
);
Arista arista = new Arista();
lados.add(arista);
...
return lados;
}
希望有帮助!
这篇关于接口中的抽象类返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!