What is the point of T extends SomeClass?

后端 未结 1 1604
既然无缘
既然无缘 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 <T extends SomeClass> void doSomething(List<T> obj)
    

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

    obj.doSomething(new ArrayList<SubclassOfSomeClass>());
    obj.doSomething(new ArrayList<SomeClass>());
    

    If you would use

    public void doSomething(List<SomeClass> obj)
    

    You would only be able to do this:

    obj.doSomething(new ArrayList<SomeClass>());
    
    0 讨论(0)
提交回复
热议问题