通常,在eclipse中,我可以运行代码而不会出现任何编译错误。然而,在使用ant文件时,我遇到了error: type argument Map is not within bounds of type-variable Type。我的ant文件有什么问题?
代码我认为是错误的;
[javac] class StratI implements Strat<Map> {
[javac] ^
[javac] where T is a type-variable:
[javac] T extends Map<?,?> declared in interface Strat
[javac] 1 errorAnt - javac部分;
<target name="compile" depends="init">
<javac compiler="javac1.7"
destdir="${build.dir}/classes"
source="1.7"
target="1.7"
includeantruntime="false"
encoding="ISO-8859-1">
<src path="${src.dir}" />
<classpath refid="classpath" />
</javac>
</target>注意:我已经在谷歌上搜索了这个错误。我没有看到任何能说明真正原因并说明如何解决它的来源。
发布于 2014-08-25 21:23:39
您的类Strat显然启动了
class Strat implements Strat<Map>显然,您不能拥有实现自身的类(因为它是一个接口;看起来您有一个名称冲突)。此外,如果没有声明为具有参数,则它不能是泛型。
因为你在不同的包中有一个通用接口(根据你的评论,是的,它在不同的包中),你可以显式地对接口使用完整的包-例如,
package com.example.interface;
// A generic Strat interface
interface Strat<T> {
}然后实现它(您可能应该选择一个更具描述性的名称-也许是IStrat<T>) -
package com.example.impl;
class Strat implements com.example.interface.Strat<Map> {
}或者使用IStrat
class Strat implements IStrat<Map> {
}发布于 2014-08-25 21:40:41
Map是一个raw-type,如错误中所述,它是is not within bounds of type-variable Type,即T extends Map<?,?>。尽管你正在扩展一个“随心所欲”的映射,但它仍然是一个原始类型的规范。
您应该在以下位置更改StratI接口实现的声明:
class StratI implements Strat<Map<?,?>>或者因为你在扩展
class StratI implements Strat<HashMap<?,?>>或者因为它是一种实现
class StratI implements Strat<HashMap<Integer, String>>编辑
Eclipse不会将其显示为错误的事实是,Eclipse不会为未参数化的原始类型显示编译错误。它会显示一条警告:
public interface myIn<T extends Map<?,?>>{
void foo();
}
public class myClass implements myIn<Map>{
@Override
public void foo() {}
}Eclipse将在Map下面加上警告:
Map是一种原始类型。对泛型类型Map<K,V>的引用应参数化为
发布于 2014-08-25 21:41:55
您定义接口的方式是错误的。请阅读java文档。下面这段代码会有所帮助。
public interface Start<T> {
public void display();
}
public class StartI implements Start<Map<String, String>> {
@Override
public void display() {
System.out.println("Implement here");
}
}https://stackoverflow.com/questions/25486800
复制相似问题