简体   繁体   中英

Java Generics Extends Class Parameter

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

public interface Shapes<T>{
//methods here
}

public class ShapeAction<T> implements Shapes<T>{
//Methods and implementations here
}

public class Circle extends ShapeAction<T>{
//Some methods here
}

The error is pointing at class Circle extends Shapes< T > where it says "T cannot be resolved to a type". If I set T to string the error will go away but that also means I can only use one datatype. What should I put inside the <> so that I can use any datatype (String, int, double etc) or did I do this the wrong way?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM