Generic class that extends class and implements interface

前端 未结 4 1202
再見小時候
再見小時候 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:48

    Do you want to do something like this?

    class SomeClass {}
    
    interface SomeInterface{
        public void someMethod();
    }
    
    class AGenericClass extends SomeClass implements SomeInterface {
        public void someMethod() {}
    }
    

    This is allowed.

    But what's Android specific about your question? I think I must be missing something so can you provide some more details?

    update below

    I'm still not totally sure what you want to do, and I'm also not sure you need to worry about the dependence right away, but this is also legal, using bounded type parameters:

    interface SomeInterface{
        public void someMethod();
    }
    
    class Fragment {
        public void aMethodInFragment() { }
    }
    
    public class SomeClass  {
        public SomeClass(T parent) {
            parent.someMethod();
            parent.aMethodInFragment();
        }
    }
    

    The type parameter specifies that T should be a subclass of Fragment and implement SomeInterface.

提交回复
热议问题