为什么会这样编译?该代码似乎打破了对类型参数的限制
在下面的测试中,TesterClass对其两个类型参数之间的关系施加了约束。方法 func2() 似乎打破了这个约束,我希望它会在某个地方导致键入编译错误(在 func2 的定义上,或者每当该类与除 String 之外的任何第二个参数一起使用时),但事实并非如此!
此外,如果我调用 func2 并将结果保存在适当类型的变量中,编译将失败(根据该变量的类型)。但是,执行相同的操作并保存为更通用的类型(例如 Object)会成功,尽管函数的返回类型在两种情况下(在向上转换之前)应该具有相同的类型。
这是怎么回事?
谢谢!
public class TestGenerics {
public static class ParamedType<T> {}
public class TesterClass<A extends ParamedType<B>, B> {
public TesterClass<A, B> func() {
return new TesterClass<A, B>();
}
public TesterClass<A, String> func2() {
return new TesterClass<A, String>();
}
}
public Object test() {
// How can I use these type parameters? Doesn't .func2 now have an invalid return type?
TesterClass<ParamedType<Integer>,Integer> testClass = new TesterClass<TestGenerics.ParamedType<Integer>, Integer>();
//TesterClass<ParamedType<String>, Integer> res2 = testClass.func2(); // <-- will not compile
Object res = testClass.func2(); // Compiles
return res;
}
}
编辑:此不能在 javac 中编译(下面报告的版本)。我正在使用 Eclipse,并试图找出实际运行的编译器是什么。会更新。可能是 JDT(Eclipse 编译器)错误。
我已经打开了 eclipse 的 jdt 的错误报告: https://bugs.eclipse.org/bugs/show_bug.cgi?id=333503
In the following test, TesterClass places a constraint on the relation between its two type parameters. The method func2() seems to break that constraint, and I expect it to cause a typing compilation error somewhere (on func2's definition, or whenever the class is used with any second parameter other than String), but it doesn't!
Furthermore, if I call func2 and save the result in an appropriately typed variable, the compilation fails (on the type of that variable). But doing the same and saving in a more general type (such as Object) succeeds, despite the fact the the function's return type should have the same type in both cases (before the up-cast).
What's going on here?
Thanks!
public class TestGenerics {
public static class ParamedType<T> {}
public class TesterClass<A extends ParamedType<B>, B> {
public TesterClass<A, B> func() {
return new TesterClass<A, B>();
}
public TesterClass<A, String> func2() {
return new TesterClass<A, String>();
}
}
public Object test() {
// How can I use these type parameters? Doesn't .func2 now have an invalid return type?
TesterClass<ParamedType<Integer>,Integer> testClass = new TesterClass<TestGenerics.ParamedType<Integer>, Integer>();
//TesterClass<ParamedType<String>, Integer> res2 = testClass.func2(); // <-- will not compile
Object res = testClass.func2(); // Compiles
return res;
}
}
EDIT: This doesn't compile in javac (versions reported below). I'm using Eclipse, and trying to find out what's the compiler that's actually running. Will update. May be a JDT (Eclipse compiler) bug.
I've opened a bug report for eclipse's jdt:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=333503
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
简单的答案:它不能编译,至少在 javac 1.7 下:
你没有说你在什么下编译它 - 我的猜测是你的 Java 编译器有一个错误。
Simple answer: it doesn't compile, at least under javac 1.7:
You didn't say what you were compiling it under - my guess is that your Java compiler has a bug in it.
显然,这是 JDT.core 中的 Eclipse bug。我已在 https://bugs.eclipse.org/bugs 打开错误报告/show_bug.cgi?id=333503
Apparently, this is an Eclipse bug in JDT.core. I've opened a bug report at https://bugs.eclipse.org/bugs/show_bug.cgi?id=333503