Java 无类型泛型类,删除其函数泛型类型
关于 javas 泛型、可迭代和 for-each 循环的好问题。问题是,如果我声明我的“测试”类为非类型化,我就会丢失所有函数的所有通用信息,而 for-each 根本不喜欢这样。
示例
public class Test<T> implements Iterable<Integer>{
public Test() {}
public Iterator<Integer> iterator() {return null;}
public static void main(String[] args) {
Test t = new Test();
//Good,
//its returning an Iterator<object> but it automatically changes to Iterator<Integer>
Iterator<Integer> it = t.iterator();
//Bad
//incompatable types, required Integer, found Object
for(Integer i : t){
}
}Untyped generic classes losing
}
当 'Test t' 为无类型时,'iterator()' 函数返回 'iterator' 而不是 'iterator <>整数>'。
我不太确定其背后的原因,我知道解决方法就是在“Test <<”上使用通配符。 ? > t = 新测试()'。然而,这并不是一个理想的解决方案。
他们有什么办法只编辑类声明及其函数并使 for every 循环工作无类型吗?
Ok Question about javas generics, iterable, and for-each loop. The problem being that, if I declare my 'Test' class untyped, I lose all generic information on all my functions and for-each is not likeing that at all.
Example
public class Test<T> implements Iterable<Integer>{
public Test() {}
public Iterator<Integer> iterator() {return null;}
public static void main(String[] args) {
Test t = new Test();
//Good,
//its returning an Iterator<object> but it automatically changes to Iterator<Integer>
Iterator<Integer> it = t.iterator();
//Bad
//incompatable types, required Integer, found Object
for(Integer i : t){
}
}Untyped generic classes losing
}
When 'Test t' is untyped, the 'iterator()' function returns 'iterator' instead of a 'iterator < Integer >'.
I'm not exactly sure for the reason behind it, I know a fix for that is just use a wild card on 'Test < ? > t = new test()'. However this is a less than ideal solution.
Is their any way to only edit the class declaration and its functions and have the for each loop work untyped?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该执行以下操作:
一起删除泛型类型。
您的
Test
类根本不通用。它只是实现一个通用接口。声明泛型类型不是必需的。这还有一个好处是可以消除您收到的一般警告。@Eugene 提出了一个很好的观点。如果您确实想要一个泛型
Test
类型,则应该将Test
声明为泛型迭代器:您应该执行以下操作:
一起删除泛型类型。
您的
Test
类根本不通用。它只是实现一个通用接口。声明泛型类型不是必需的。这还有一个好处是可以消除您收到的一般警告。然后,确保在实例化
Test
时将其设为通用。然后对
for(Integer i: t)
的调用将进行编译。You should just do the following:
Remove the generic type all together.
Your
Test
class is not generic at all. It is simply implementing a generic interface. Declaring a generic type is not necessary. This will also have the benefit of remove that generic warning you were getting.@Eugene makes a good point. If you actually wanted a generic
Test
type, you should declareTest
as a generic iterator:You should just do the following:
Remove the generic type all together.
Your
Test
class is not generic at all. It is simply implementing a generic interface. Declaring a generic type is not necessary. This will also have the benefit of remove that generic warning you were getting.And then, make sure you make
Test
generic when you instantiate it.Then calls to
for(Integer i: t)
will compile.你应该这样写:
或者实际上泛化你的类:
You should either write this:
or actually generify your class: