泛型:
好处:(1)提高安全性,将运行期间的错误转换到编译期.(2)省去强转的麻烦.
使用:<>中放的必须是引用数据类型
注意:前后的泛型必须一致,或者后面的泛型可以省略不写,泛型最好不要定义成Object,没有意义
泛型类:把泛型定义在类上
泛型方法:方法泛型需要于类的泛型一致
如果不一致,需要在方法上声明该泛型,用的时候传具体的值
public<Z> void show2(Z z){
System.out.println(q);
}
静态方法随着类的加载而加载,所以必须声明自己的泛型
public static <R> void print(R r){
}
泛型接口:把泛型定义到接口上
publi interface 接口名<泛型类型>
泛型的通配符<?>
List<?> list4=new ArrayList<String>(); //右边泛型是不确定时,左边可以指定为?
? extends E ?是子类,E是父类(向下限定,E及其子类)
? super E ?是子类,E是父类(向上限定,E及其父类)
package pra_10;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class J_20 {
public static void main(String[] args){
ArrayList<People2> list=new ArrayList<People2>();
list.add(new People2("aa",11));
Iterator<People2> it=list.iterator(); //获取迭代器
while(it.hasNext()){
People2 p=it.next();
System.out.println(p.getName()+"..."+p.getAge());
}
//ArrayList利用泛型存储字符串和自定义对象并遍历
ArrayList<String> list2=new ArrayList<>(); //创建集合对象
list2.add("b");
list2.add("c");
list2.add("d");
list2.add("e");
Iterator it2=list2.iterator();
while(it2.hasNext()){
System.out.println(it2.next());
}
ArrayList<People> list3=new ArrayList<People>();
list3.add(new People("qq",11));
list3.add(new People("zz",22));
list3.add(new People("aa",33));
Iterator<People> it3=list3.iterator();
while(it3.hasNext()){
People p2=it3.next();
System.out.println(p2.getName()+"..."+p2.getAge());
}
//泛型类的初步使用
People3<String> p3=new People3<>();
p3.show("abc"); //abc
p3.show2(true); //true
//泛型的通配符<?> ?代表任意类型
List<?> list4=new ArrayList<String>(); //右边泛型是不确定时,左边可以指定为?
ArrayList<People2> list5=new ArrayList<>();
list5.add(new People2("bb",11));
list5.add(new People2("vv",22));
list5.add(new People2("dd",33));
list5.add(new People2("qq",44));
ArrayList<People5> list6=new ArrayList<>();
list6.add(new People5("ll",55)); //People5必须继承People2
list6.add(new People5("mm",66));
list5.addAll(list6); //向下限定
System.out.println(list5);
}
}
class People2{
private String name;
private int age;
public People2() {
super();
}
public People2(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "People2 [name=" + name + ", age=" + age + "]";
}
//重写的方法在这!!!
public boolean equals(Object obj) {
People2 p=(People2)obj;
return this.name.equals(p.name)&&this.age==p.age;
}
}
class People5 extends People2{
private String name;
private int age;
public People5() {
super();
}
public People5(String name, int age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String toString() {
return "People5 [name=" + name + ", age=" + age + "]";
}
//重写的方法在这!!!
public boolean equals(Object obj) {
People5 p=(People5)obj;
return this.name.equals(p.name)&&this.age==p.age;
}
}
//往Q赋什么类型就是什么类型
class People3<Q>{
private Q q;
public Q getQ() {
return q;
}
public void setQ(Q q) {
this.q = q;
}
public void show(Q q){
System.out.println(q);
}
public<Z> void show2(Z z){ //与类泛型不一致的方法泛型
System.out.println(z);
}
public static <R> void print(R r){ //静态方法随着类的加载而加载,所以必须声明自己的泛型
System.out.println(r);
}
}
interface Inter<T>{
public void show(T t);
}
//方法1 推荐用
class In implements Inter<String>{
@Override
public void show(String t) {
System.out.println(t);
}
}
//方法2
class In2<T> implements Inter<T>{ //In2类有泛型<T>,实现接口也有一个泛型<T>,没必要在实现接口时给自己类加泛型
@Override
public void show(T t) {
System.out.println(t);
}
}