测试类
package Genericity;import java.util.ArrayList;public class test {public static void main(String[] args) {// 使用泛型方法添加元素ArrayList<String> list = new ArrayList<>();MyToolClass.ListAdd(list,"fdsf","dsfa");System.out.println(list);//[fdsf, dsfa]//创建了一个泛型方法实现多个数据的一次性录入ArrayList<Integer> list2 = new ArrayList<>();MyToolClass.ListAdd(list2,1,2);System.out.println(list2);//[fdsf, dsfa]// 重载后的任意参数个数的泛型方法MyToolClass.ListAdd(list2,3,4,5,4,9,7);System.out.println(list2);//[1, 2, 3, 4, 5, 4, 9, 7]// 泛型接口的两种使用方式// 实现类给定类型,类名后面不用加泛型MyToolClass2 tool2 = new MyToolClass2();tool2.size();//MyToolClass2 size// 泛型类,新建对象的时候要指定类型MyToolClass3<String> tool3 = new MyToolClass3<>();tool3.size();//MyToolClass2 size// 泛型的通配符 ? extends E 用来限定继承的抽象方法使用范围ArrayList<Dog> d1 = new ArrayList<>();ArrayList<Dog> d2 = new ArrayList<>();ArrayList<Bird> b1 = new ArrayList<>();ArrayList<Bird> b2 = new ArrayList<>();//使用通配符继承Animal类,子类的集合对象都可以调用actAnimal(d1); actAnimal(d2); actAnimal(b1); actAnimal(b2);//使用通配符继承Dog类,除了Dog类型的集合对象和Dog的子类集合对象,其他的集合对象都不能调用actDog(d1); actDog(d2);//不兼容的类型: actDog(b1); actDog(b2);}// 泛型的通配符 ? extends E 用来限定继承的抽象方法public static void actAnimal(ArrayList<? extends Animal> animals) {System.out.println("我是一个动物");}public static void actDog(ArrayList<? extends Dog> dogs) {System.out.println("我是一条狗");}
}
工具类
MyToolClass
package Genericity;import java.util.ArrayList;public class MyToolClass {MyToolClass() {}// 创建一个泛型方法实现多个数据的一次性录入// 固定参数的泛型方法public static<E> void ListAdd(ArrayList<E> list, E a, E b) {list.add(a);list.add(b);}// 重载后的任意参数个数的泛型方法public static<E> void ListAdd(ArrayList<E> list, E...a) {for(E e : a){list.add(e);}}}
MyToolClass2
package Genericity;import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
// 实现类给定类型,类名后面不用加泛型
public class MyToolClass2 implements List<Integer> {@Overridepublic int size() {System.out.println("MyToolClass2 size");return 0;}@Overridepublic boolean isEmpty() {return false;}@Overridepublic boolean contains(Object o) {return false;}@Overridepublic Iterator<Integer> iterator() {return null;}@Overridepublic Object[] toArray() {return new Object[0];}@Overridepublic <T> T[] toArray(T[] a) {return null;}@Overridepublic boolean add(Integer integer) {return false;}@Overridepublic boolean remove(Object o) {return false;}@Overridepublic boolean containsAll(Collection<?> c) {return false;}@Overridepublic boolean addAll(Collection<? extends Integer> c) {return false;}@Overridepublic boolean addAll(int index, Collection<? extends Integer> c) {return false;}@Overridepublic boolean removeAll(Collection<?> c) {return false;}@Overridepublic boolean retainAll(Collection<?> c) {return false;}@Overridepublic void clear() {}@Overridepublic Integer get(int index) {return 0;}@Overridepublic Integer set(int index, Integer element) {return 0;}@Overridepublic void add(int index, Integer element) {}@Overridepublic Integer remove(int index) {return 0;}@Overridepublic int indexOf(Object o) {return 0;}@Overridepublic int lastIndexOf(Object o) {return 0;}@Overridepublic ListIterator<Integer> listIterator() {return null;}@Overridepublic ListIterator<Integer> listIterator(int index) {return null;}@Overridepublic List<Integer> subList(int fromIndex, int toIndex) {return List.of();}
}
MyToolClass3
package Genericity;import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
// 泛型类,新建对象的时候要指定类型
public class MyToolClass3<E> implements List<E> {@Overridepublic int size() {System.out.println("MyToolClass3 size");return 0;}@Overridepublic boolean isEmpty() {return false;}@Overridepublic boolean contains(Object o) {return false;}@Overridepublic Iterator<E> iterator() {return null;}@Overridepublic Object[] toArray() {return new Object[0];}@Overridepublic <T> T[] toArray(T[] a) {return null;}@Overridepublic boolean add(E e) {return false;}@Overridepublic boolean remove(Object o) {return false;}@Overridepublic boolean containsAll(Collection<?> c) {return false;}@Overridepublic boolean addAll(Collection<? extends E> c) {return false;}@Overridepublic boolean addAll(int index, Collection<? extends E> c) {return false;}@Overridepublic boolean removeAll(Collection<?> c) {return false;}@Overridepublic boolean retainAll(Collection<?> c) {return false;}@Overridepublic void clear() {}@Overridepublic E get(int index) {return null;}@Overridepublic E set(int index, E element) {return null;}@Overridepublic void add(int index, E element) {}@Overridepublic E remove(int index) {return null;}@Overridepublic int indexOf(Object o) {return 0;}@Overridepublic int lastIndexOf(Object o) {return 0;}@Overridepublic ListIterator<E> listIterator() {return null;}@Overridepublic ListIterator<E> listIterator(int index) {return null;}@Overridepublic List<E> subList(int fromIndex, int toIndex) {return List.of();}
}
泛型的通配符测试对应的类
Animal父类
package Genericity;public abstract class Animal {private String name;abstract void Act();public String getName() {return name;}public void setName(String name) {this.name = name;}public Animal(String name) {this.name = name;}public Animal() {}
}
Dog子类
package Genericity;public class Dog extends Animal {public Dog() {}public Dog(String name) {super(name);}@Overridevoid Act() {System.out.println("会摇尾巴");}
}
Bird子类
package Genericity;public class Bird extends Animal{public Bird() {}public Bird(String name) {super(name);}@Overridevoid Act() {System.out.println("会飞");}
}