List<String> strings = new ArrayList<String>();
strings.add("1");
本质:参数化类型
泛型的擦除
泛型只在编译阶段有效,编译之后JVM会采取去泛型化的措施;
泛型在运行阶段是没有效果的;
通配符
1.无边界通配符
?= anything都可以
public class Demo1 {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("剑来");
loop(list);
}
public static void loop(List<?> list) {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
剑来
2.上边界通配符
<? extends Number> 代表从Number往下的子类或孙类对象都可以使用
public static void main(String[] args) {
List<Double> list = new ArrayList<Double>();
list.add(1D);
loop(list);
List<String> string = new ArrayList<String>();
string.add("1");
loop(string); // 次数异常!
}
public static void loop(List<? extends Number> list) {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
3.下边界通配符
<? supper Integer> 通用类似必须是Integer到Object类型的对象
<? supper Number> 通用类似必须是Number到Object类型的对象
public static void main(String[] args) {
List<Integer> list = new ArrayList<Integer>();
list.add(1);
loop(list);
List<Object> objects = new ArrayList<Object>();
objects.add(1D);
loop(objects);
List<String> strings = new ArrayList<String>();
strings.add("1"); // 这个报错
loop(strings);
}
public static void loop(List<? super Integer> list) {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
实际使用
泛型类
class Person
public class PeresonBean {
private long id;
private String name;
private String age;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
public class PersonNew<T> {
private T t;
public T getT() {
return t;
}
public void setT(T t) {
this.t = t;
}
public PersonNew(T t) {
this.t = t;
}
}
public class Demo5 {
public static void main(String[] args) {
Person p = new Person();
p.setIdCard("11");
PeresonBean peresonBean = new PeresonBean();
peresonBean.setId(1);
peresonBean.setAge("12");
peresonBean.setName("1");
// 增加了灵活度
PersonNew<PeresonBean> personNew = new PersonNew< >(peresonBean);
PersonNew<String> stringPersonNew = new PersonNew< >("剑来");
}
}
泛型方法
public class Demo6<K, V> {
//强制转换
/**
* 强制转换 可使用类中定义的泛型
*
* @param k
* @param v
* @return
*/
public K method(K k, V v) {
return (K) null;
}
/**
* 方法中泛型
*
* @param <T>
* @return
*/
public <T> T method2() {
return (T) null;
}
/**
* 静态方法中我们无法使用类中定义的泛型
*/
public static k method3
{
return null;
}
}
泛型接口
接口,类都能使用
public interface CalGeneric<T> {
T add(T a, T b);
T sub(T a, T b);
T mul(T a, T b);
T div(T a, T b);
}
public class CalculatorDoubleGeneric implements CalGeneric<Double> {
@Override
public Double add(Double a, Double b) {
return null;
}
@Override
public Double sub(Double a, Double b) {
return null;
}
@Override
public Double mul(Double a, Double b) {
return null;
}
@Override
public Double div(Double a, Double b) {
return null;
}
}
public class CalculatorIntegerGeneric implements CalGeneric<Integer> {
@Override
public Integer add(Integer a, Integer b) {
return null;
}
@Override
public Integer sub(Integer a, Integer b) {
return null;
}
@Override
public Integer mul(Integer a, Integer b) {
return null;
}
@Override
public Integer div(Integer a, Integer b) {
return null;
}
}
3