Generic class that extends class and implements interface

前端 未结 4 1218
再見小時候
再見小時候 2021-01-31 06:59

To reduce dependece of class, I want to send parameter (using generic class) to constructor that extends some class and implements interface, for example

public          


        
4条回答
  •  不知归路
    2021-01-31 07:53

    T must extend Fragment and implement SomeInterface

    In that case you could declare SomeClass as the following:

    public class SomeClass
    

    That would require an object of type T to both extend Fragment and implement SomeInterface.

    Further, using my T class, I want to create views, using T.getActivity() as Context.

    I'm unfamiliar with Android, but if getActivity() is a public instance method declared in Fragment then will be entirely possible to call it on an instance of T, since the compiler will know all Ts must inherit that method.

提交回复
热议问题