What is the point of T extends SomeClass?

后端 未结 1 1615
既然无缘
既然无缘 2020-12-06 09:47

What is the difference with a method declaration like this:

public  void doSomething(T obj)
{
    // Do something.
}
1条回答
  •  抹茶落季
    2020-12-06 10:01

    In your case it doesn't make much difference.

    But consider the following:

    public  void doSomething(List obj)
    

    In this case you could call the method the following ways:

    obj.doSomething(new ArrayList());
    obj.doSomething(new ArrayList());
    

    If you would use

    public void doSomething(List obj)
    

    You would only be able to do this:

    obj.doSomething(new ArrayList());
    

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