[英]Java Generics and generic types
I have a class ExtA which contains a filter function to filter an ArrayList: 我有一个ExtA类,其中包含一个用于过滤ArrayList的过滤器函数:
public ExtA<T> filt(...)
{
//code
}
when I compile it is giving me error: cannot find symbol- class T. Why is this? 编译时出现错误:找不到符号类T。这是为什么?
You have to tell it, that T is a generic type in this case: 您必须告诉它,在这种情况下T是泛型类型:
public <T> ExtA<T> filt(Func<T, Boolean> a)
You declared your interface with the symbol T
, but that symbol is only valid in the interface declaration itself. 您使用符号T
声明了接口,但是该符号仅在接口声明本身中有效。 The T
you are using in your method is a different T
. 您在方法中使用的T
是不同的T
You have to declare it again, since the method is not implemented inside the interface declaration. 您必须再次声明它,因为该方法未在接口声明中实现。
You have to put the parameter on the method: 您必须将参数放在方法上:
public <T> ExtA<T> filt(Func<T, Boolean> a) {
// method code
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.