Java Generics Extends Class Parameter

前端 未结 3 1450
挽巷
挽巷 2021-01-20 08:26

I received an error and I have this structure in my program

public interface Shapes{
//methods here
}

public class ShapeAction implements          


        
3条回答
  •  孤街浪徒
    2021-01-20 09:06

    Have you tried this?

    public class Circle extends ShapeAction{
    //Some methods here
    }
    

    There is no way the compiler can tell what the T argument means in extends ShapeAction unless you define it first.

    Eventually, when you use a reference to a Circle, you will have to provide a type argument for T, which will be cascaded to all other appearances of this type.

    Circle myInt;
    

    Will cause the compiler to interpret the code as:

    public class Circle extends ShapeAction{
        //Some methods here
    }
    

    And so on, for the rest of the class hierarchy.

    As such, if you do not define the type T in the class Circle. There wouldn't be a way for the compiler to tell the parameterized type of ShapeAction and this is most likely the source of the problem here.

提交回复
热议问题