泛型是jdk1.5中引入的特性,它提供了编译时类型安全检测机制。
泛型的好处:
- 把运行时的问题提前到了编译期间。
- 避免了强制类型转换
-
public class GenericitySummarize { public static void main(String[] args) { List list = new ArrayList<>(); //默认是object list.add("aaa"); list.add("bbb"); list.add("ccc"); //假如写了泛型,这个123就添加不进去 list.add(123); list.add(0.12f); list.add(8.1d); list.add(15L); //假如泛型规定了,那么add的肯定是同一种类型,不然就报错了。 //迭代器 Iterator iterator = list.iterator(); while (iterator.hasNext()) { //自动生成的左边也是object。 //如果写了泛型,就不需要强转了 String next = (String)iterator.next(); int length = next.length(); System.out.println("next = " + next); } } }
不用泛型导致的错误:报错的原因是,装箱后就是Integer类型,将Integer类型强转为String类型,显然不合逻辑。
注意:list自动装箱,如下:
泛型的定义泛型可以使用的地方:
类后面 ---------泛型类
方法声明上----------泛型方法
接口上----------------泛型接口
泛型类:如果一个类的后面有
创建泛型类的对象时,必须要给这个泛型确定具体的数据类型。
自定义泛型类。(给别人使用)public class MyGenericityClass {
public static void main(String[] args) {
Box box1 = new Box<>();
box1.setElement("西游记");
System.out.println(box1.getElement());
//和创建的普通类没区别。
Box box2 = new Box<>();
box2.setElement(1);
System.out.println(box2.getElement());
}
}
//就是一个泛型类,相当于一个容器,用来装东西 public class Box泛型方法{ //但是装的类型不确定,该怎么写呢? private E element; public E getElement() { return element; } public void setElement(E element) { this.element = element; } }
要使用字符串里面的方法,就得强转,非常不方便。
public class MyGenericityMethod {
public static void main(String[] args) {
List list = new ArrayList<>();
list.add("在越秀山");
list.add("在白云山");
list.add("在火炉山");
//将list转化为一个数组并输出
//这个方法遍历出来的是Object类型,如果需要用到字符串的方法,还需要强转,非常不方便
Object[] objects = list.toArray();
//Arrays.toString() 将数组里面的值 打印出来。
//也可以用增强for
System.out.println(Arrays.toString(objects));
//修改,需要传一个T类型的对象数组 T[] a
String[] strings = list.toArray(new String[list.size()]);
System.out.println(Arrays.toString(strings));
}
}
打印结果:
泛型方法泛型方法的定义格式:
- 格式:修饰符<类型> 返回值类型方法名(类型 变量名){}
- 范例:public
void show(T t){} -
private static
List printList(List list){return null;}
public class CustomerGenericityMethod {
public static void main(String[] args) {
List show = show(new ArrayList(), "a", "b", "c", "d");
System.out.println(show);
//假如需求变了,
List showInteger = show(new ArrayList(), 1, 2, 3, 4);
System.out.println(showInteger);
}
//技巧:先写一个普通方法,再修改为一个泛型方法(T在返回值的前面)
public static List show(List list ,T t1,T t2,T t3,T t4){
list.add(t1);
list.add(t2);
list.add(t3);
list.add(t4);
return list;
}
}
泛型接口
泛型接口的使用方式:
- 实现类也不给泛型
- 实现类确定具体的数据类型
泛型接口的定义格式:
- 格式:修饰符interface 接口名<类型>{}
- 范例:public interface Generic
{}
主方法:
public class GenericityInterface {
public static void main(String[] args) {
Genericity genericity = new GenericityImpl();
genericity.method("a");
//普通类,不是泛型类
GenericityImpl2 ge = new GenericityImpl2();
ge.method(1);
}
}
定义接口:
//注意:接口中默认的变量是:public static final XX //默认的方法是:public abstract XXX public interface Genericity{ void method(E e); }
//实现类这个泛型没给出具体的类型,就要从接口那里延续下来 public class GenericityImplimplements Genericity { @Override public void method(E e) { System.out.println(e); } }
//实现类这个泛型没给出具体的类型,就要从接口那里延续下来 public class GenericityImpl2 implements Genericity泛型通配符:{ @Override public void method(Integer integer) { System.out.println(integer); } }
- 类型通配符:> ,?表示通配符,谁来都是可以的。
- ArrayList>:表示元素类型未知的ArrayList,它的元素可以匹配任意的类型
- 但是并不能把元素添加到ArrayList中了,获取出来的也object类型
- 类型通配符上限: extends 类型>
- 比如:ArrayList extends Number>:它表示的类型是Number或者其子类型
- 类型通配符下限: super 类型>
- 比如:ArrayList super Number>:它表示的类型是Number或者其父类型
-
public class genericityglobbing1 { public static void main(String[] args) { ListlistStr = new ArrayList<>(); List listInt = new ArrayList<>(); List



