简体   繁体   中英

Java Generics and generic types

I have a class ExtA which contains a filter function to filter an ArrayList:

public ExtA<T> filt(...)
{

 //code


}

when I compile it is giving me error: cannot find symbol- class T. Why is this?

You have to tell it, that T is a generic type in this case:

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. The T you are using in your method is a different 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM