泛型
<>内的都算泛型,规定传参格式,还有一种是可以规定类的格式.
public class student <E>{
private E name //所有格式都可以传入,只有在创建对象时定义格式。
public E setName(E e){ //这个是泛型方法,参数什么类型就返回什么类型
this.name=e;
}
}
main 函数{
student <String>stu =new student();
stu.setName("asa");
student<Interger> s= new student();
s.setName(1); //字符串和数组都可以作为名字。
}
接口也可以使用泛型,实现类需要跟接口泛型一致。
泛型通配符?
void show (ArrayList<?> list ){
Interat <?> i=list.interat();
while(i.hasnext()){
Object o=i.next(); //object 接受一切。
print(o);
}
}