Java Generics Extends Class Parameter

前端 未结 3 1478
挽巷
挽巷 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: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  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.

提交回复
热议问题