目录
一、 泛型类
1. 父类Person与子类Teacher
Person类:使用泛型
Teacher类:
方式一:使用泛型并继承Person:不能使用父类类型参数
方式二:继承泛型使用泛型:可使用父类类型参数
测试类:
结果:
二、 泛型方法
Factory类:实例制造器
测试类:
结果:
核心代码:
三、 泛型接口
CarFactory类:
测试类:
结果:
三、 通配符
代码:
结果:
四、 通配符的捕获
Tool类:
测试类:
结果:
package fanxing; /** * @author: y9 * @Date: 2021/10/02 */ public class Person<T> { private T t; public Person(T t) { this.t = t; } public String toString() { return "参数的类型是:" + t.getClass().getCanonicalName(); } }
package fanxing; /** * @author: y9 * @Date: 2021/10/02 */ public class Teacher <V,S> extends Person{ protected V v; private S s; public Teacher(Object obj) { super(obj); //传入Person的泛型T } public void set(V v,S s) { this.v = v; this.s = s; } }
package fanxing; /** * @author: y9 * @Date: 2021/10/02 */ public class Teacher <T,S> extends Person{ protected T t; //使用父类泛型<T> private S s; public Teacher(T t) { super(t); } public void set(T t,S s) { this.t = t; this.s = s; } }
package fanxing; public class GenericTest <T>{ public static void main(String[] args) { Person<Integer> p = new Person<Integer>(5); System.out.println(p.toString()); } }
参数的类型是:java.lang.Integer
package fanxing; /** * @author: y9 * @Date: 2021/10/02 */ public class Factory { //实例生产器 public <T>T generator(Class<T> t)throws Exception{ return t.newInstance(); } }
package fanxing; import java.awt.Button; import java.util.Date; /** * @author: y9 * @Date: 2021/10/02 */ public class GenericTest <T>{ public static void main(String[] args) throws Exception { Factory factory = new Factory(); Date date = factory.generator(Date.class); //获取Date类 System.out.println(date.toString()); Button button = factory.generator(Button.class); //获取Button类 System.out.println(button.toString()); } }
Sat Oct 02 09:53:41 CST 2021 java.awt.Button[button0,0,0,0x0,invalid,label=]
public <T>T generator(Class<T> t)throws Exception{ return t.newInstance(); }
Date date = factory.generator(Date.class); //获取Date类 System.out.println(date.toString());
package fanxing; /** * @author: y9 * @Date: 2021/10/02 */ public class CarFactory implements Factory<Car>{ @Override public Car create() { System.out.println("制车厂:装载发动机、座椅、轮子"); return new Car(); } }
package fanxing; import java.awt.Button; import java.util.Date; /** * @author: y9 * @Date: 2021/10/02 */ public class GenericTest <T>{ public static void main(String[] args) throws Exception { Factory<Car> carFac = new CarFactory(); Factory<Computer> comFac = new ComputerFactory(); System.out.println("=====开始生产车====="); carFac.create(); System.out.println("=====开始生产电脑====="); comFac.create(); } }
=====开始生产车===== 制车厂:装载发动机、座椅、轮子 =====开始生产电脑===== 电脑厂:装载主板、CPU
<?>:不确定的类型。可以随心改类型。
package fanxing; import java.util.Date; /** * @author: y9 * @Date: 2021/10/02 */ public class GenericTest <T>{ public static void main(String[] args) throws Exception { Class<?> clazz = Integer.class; System.out.println(clazz.getCanonicalName()); clazz = String.class; System.out.println(clazz.getCanonicalName()); clazz = Date.class; System.out.println(clazz.getCanonicalName()); } }
java.lang.Integer java.lang.String java.util.Date
package fanxing; import java.util.List; /** * @author: y9 * @Date: 2021/10/02 */ public class Tool { public<T> void exchangeT(List<T> list, int i, int j) { T t = list.get(i); list.set(i, list.get(j)); list.set(j, t); } }
package fanxing; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * @author: y9 * @Date: 2021/10/02 */ public class GenericTest <T>{ public static void main(String[] args) throws Exception { Tool tool = new Tool(); List<Integer> list = new ArrayList<Integer>(); list.add(3); list.add(4); System.out.println(list.get(0) + " " + list.get(1)); tool.exchangeT(list, 0, 1); System.out.println(list.get(0) + " " + list.get(1)); } }
3 4 4 3