Difference between Class and Class<?>

后端 未结 4 2100
长情又很酷
长情又很酷 2021-02-01 13:10

What is the difference between a Class and a Class declaration.

  • Class a;
  • Class b;
4条回答
  •  执念已碎
    2021-02-01 13:48

    "it look like old code or code written by someone who hasn't learned generics yet." This is an correct statement. Class (pronounced "class of unknown"), that is, a class whose type matches anything. It's called a wild-card type for obvious reasons.

    for example:

    public void drawAll(List shapes) {
        for (Shape s: shapes) {
            s.draw(this);
       }
    }
    

    type rules say that drawAll() can only be called on lists of exactly Shape: it cannot, for instance, be called on, say, a List. That is unfortunate, since all the method does is read shapes from the list, so it could just as well be called on a List.

    What we really want is for the method to accept a list of any kind of shape.

    public void drawAll (List  shapes) {
    
    }
    

    read more: http://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html

提交回复
热议问题