Java中T和?的区别

T和?的区别
@Test
public void testOne(){

// ?做对象的泛型来用
List list = findAllList( 1 );
list.forEach(temp ->{
System. out .print(temp);
});
System. out .println();
List lists = findAllList( 2 );
lists.forEach(temp ->{
System. out .print(temp);
});

//T做类的泛型,当输入的类型是One的时候,只能用One接
One one = new Class().newInstance();
//而不能用Two接
One one = new Class().newInstance();

}

class Two{
private Two(){
System. out .println( "Two" );
}
}

static class Class< T >{
T newInstance() {
return ( T ) new One();
}


}

private static class One{
private One(){
System. out .println( "One" );
}
}

private List findAllList(Integer type){
if (type == 1 ){
List list = new ArrayList<>();
list.add( 1 );
list.add( 2 );
return list;
} else {
List list = new ArrayList<>();
list.add( "AA" );
list.add( "BB" );
return list;
}

}

你可能感兴趣的:(java开发)