我的interface在interface中有以下层次结构
public interface Identifiable<T extends Comparable<T>> extends Serializable {
public T getId();
}
public interface Function extends Identifiable {
public String getId();
}
public abstract class Adapter implements Function {
public abstract String getId();
}当我尝试在Adapter中实现scala时,如下所示
class MultiGetFunction extends Adapter {
def getId() : String = this.getClass.getName
}我收到了跟随错误
Multiple markers at this line
- overriding method getId in trait Identifiable of type ()T; method getId has incompatible
type
- overrides Adapter.getId
- implements Function.getId
- implements Identifiable.getId发布于 2014-01-31 06:29:07
一般来说,在Scala的java代码中使用原始类型是很痛苦的。
尝试以下几点:
public interface Function extends Identifiable<String> {
public String getId();
}错误可能是由于编译器无法确定T类型,因为在声明Function extends Identifiable时没有提到任何类型。这是从错误中解释的:
:17:错误:重写可识别类型()T的特征的方法getId;getId方法具有不兼容的类型
Scala与Java1.5及更高版本兼容。对于以前的版本,您需要进行黑客攻击。如果无法更改Adapter,则可以在Java中创建Scala包装器:
public abstract class ScalaAdapter extends Adapter {
@Override
public String getId() {
// TODO Auto-generated method stub
return getScalaId();
}
public abstract String getScalaId();
}然后在Scala中使用这个:
scala> class Multi extends ScalaAdapter {
| def getScalaId():String = "!2"
| }
defined class Multihttps://stackoverflow.com/questions/21473614
复制相似问题