Java generics and varargs

后端 未结 6 1943
走了就别回头了
走了就别回头了 2020-12-05 00:19

I\'d like to implement a function with both generics and varargs.

public class Question {
    public static  void doNastyThingsToClasses(Class

        
6条回答
  •  时光取名叫无心
    2020-12-05 00:34

    The second argument Class... that must extend the class that the first argument is (ex. argument one is a Question so the second argument be something that extends Question.

    The Breakdown:
    NastyThingsToClasses(Object.class, Question.class, SomeQuestion.class); // OK
    Everything extends Object so the second argument is correct.

    NastyThingsToClasses(Question.class, SomeQuestion.class); // OK
    SomeQuestion extends Question so thats fair game.

    NastyThingsToClasses(Question.class, Object.class, SomeQuestion.class);
    Object does not extend Question hence error.


    hopefully that cleared things up.

    -Brett

提交回复
热议问题