Java Generics and generic types

后端 未结 2 1317
半阙折子戏
半阙折子戏 2021-01-16 05:01

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

public ExtA filt(...)
{

 //code


}

when I compile i

相关标签:
2条回答
  • 2021-01-16 05:33

    You have to put the parameter on the method:

    public <T> ExtA<T> filt(Func<T, Boolean> a) {
    // method code
    }
    
    0 讨论(0)
  • 2021-01-16 05:53

    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.

    0 讨论(0)
提交回复
热议问题