泛型和继承

3

鉴于现状:

class School 
{
    public void print(){}
}

class Grade extends School{}

class Student extends School{}

什么是这个的区别。
public <E extends School> void someMethod(E someObj) 
{
    //now we can pass any object that extends school 
    //and we have access to any methods that are in school
    someObj.print();

}

还有这个呢?

public void someMethod(School someObj)
{
    someObj.print();
}
3个回答

3

显而易见的区别是一个是通用的,另一个则不是。

另外一个知道的事情是通用的那个可以捕获参数的实际类型。当然,在这种情况下并没有什么区别。但是想象一下,如果您希望返回类型是参数的确切类型,那么它就会变得重要。


1
在这种情况下,没有区别,因为类型参数只在一个地方用作参数的类型。类型参数仅在更复杂的情况下才是必需的。

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接