Java Generics Extends Class Parameter

前端 未结 3 1446
挽巷
挽巷 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<T> extends ShapeAction<T>{
    //Some methods here
    }
    

    There is no way the compiler can tell what the T argument means in extends ShapeAction<T> 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<Integer> myInt;
    

    Will cause the compiler to interpret the code as:

    public class Circle<Integer> extends ShapeAction<Integer>{
        //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.

    0 讨论(0)
  • 2021-01-20 09:07

    There are two different concepts. When you write

    public class ShapeAction<T>
    

    this means that you are creating a class which, when instantiated, will be parametrized with some class. You don't know at the time which it will be, so you refer to it just as T.

    But when you write

    public class Circle extends ShapeAction<T>
    

    this means that you want Circle to be a subclass of ShapeAction parametrized with type T. But what is T? Compiler can't tell that: you declared Circle without any type variables.

    You have two options. You can make Circle generic too:

    public class Circle<T> extends ShapeAction<T>
    

    This way when you make a new instance of Circle you specify what type it works with, and this extends to the superclass.

    And what if you want to specify that ShapeAction can be of any type, but without making the subclass generic? Use Object:

    public class Circle extends ShapeAction<Object>
    

    This way Circle stays non-generic, but you can use any data types with the superclass.

    0 讨论(0)
  • 2021-01-20 09:07

    You haven't really shown us enough code to tell what you're trying to achieve, but in general:

    If you want to make a method that can accept multiple types of arguments, you want to parameterize the method, not the entire class.

    public <T> Shape takeAction(T argument);
    

    Obviously that doesn't really offer much compile time type safety, you'll still probably have to manually resolve the argument to some subset of restricted types at runtime.

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