高级特性
泛型
-
集合中应用泛型,约束集合元素的类型
-
<K,V> 泛型符号 E K V T ;泛型符号的名称无要求,泛型符号的个数无要求
-
泛型符号应用在类,接口,方法的定义;泛型类,泛型接口,泛型方法
-
泛型符号其实是个占位符,占个位置,给引用类型占位置;
-
泛型符号如果不指定类型,统统当成Object来看;
-
泛型符号在类中,接口中,方法中 都当成已知类型来用
1.1.泛型类
-
泛型符号应用在类的定义上
-
泛型符号在本类中当成已知类型来进行使用
-
在创建对象的时候 确定类型;如果不指定类型,统统当成Object来看;
类型定义
//泛型类;泛型符号在本类中当成已知类型来进行使用
public class GenericCustom<A, B, C> {
private A a;
private B b;
private C c;
public GenericCustom() {
}
public GenericCustom(A a, B b, C c) {
this.a = a;
this.b = b;
this.c = c;
}
public A getA() {
return a;
}
public void setA(A a) {
this.a = a;
}
public B getB() {
return b;
}
public void setB(B b) {
this.b = b;
}
public C getC() {
return c;
}
public void setC(C c) {
this.c = c;
}
}
测试代码
public static void main(String[] args) {
GenericCustom<String, Double, Integer> genericCustom1 = new GenericCustom<>();
String a = genericCustom1.getA();
GenericCustom<Double, Byte, String> genericCustom2 = new GenericCustom<>();
Double a1 = genericCustom2.getA();
//泛型符号如果不指定类型,统统当成Object来看
GenericCustom genericCustom3 = new GenericCustom();
GenericCustom<Object, Object, Object> genericCustom4 = new GenericCustom();
// GenericCustom<String, Double> genericCustom5 = new GenericCustom();
Object obj = new Object();
Student<String> stu = new Student();
//集合类型都是泛型类
ArrayList<String> arrayList1 = new ArrayList<>();
ArrayList arrayList2 = new ArrayList<>();
String s = arrayList1.get(0);
HashMap<String, String> hashMap = new HashMap<>();
HashMap<Integer, String> hashMap2 = new HashMap<>();
}
1.2.泛型接口
-
泛型符号 应用在接口 的定义上,这个接口 称之为 泛型接口
接口定义
//泛型符号在本接口 已知类型来使用
public interface GenericInter<K, V> {
K methodK(K k);
V methodV(V v);
}
实现类1
//1 ,实现类直接确定类型
public class GenericInterImpl1 implements GenericInter<Double, String> {
@Override
public Double methodK(Double aDouble) {
return null;
}
@Override
public String methodV(String s) {
return null;
}
}
//如果不指定类型,统统当成Object来看;
public class GenericInterImpl2 implements GenericInter {
@Override
public Object methodK(Object o) {
return null;
}
@Override
public Object methodV(Object o) {
return null;
}
}
实现类2
//2,实现类继续带上泛型符号
public class GenericInterImpl3<K, V> implements GenericInter<K, V> {
private K k;
private V v;
@Override
public K methodK(K k) {
return null;
}
@Override
public V methodV(V v) {
return null;
}
}
测试类
public class Test2 {
public static void main(String[] args) {
GenericInterImpl1 genericInterImpl1 = new GenericInterImpl1();
GenericInterImpl2 genericInterImpl2 = new GenericInterImpl2();
GenericInterImpl3<String,Double> genericInterImpl3 = new GenericInterImpl3<>();
String s = genericInterImpl3.methodK("");
GenericInterImpl3 genericInterImpl4 = new GenericInterImpl3<>();
Object o = genericInterImpl4.methodK("");
}
}
1.3.泛型方法
-
泛型符号 单独使用在方法上面,泛型方法
public class GenericMethod {
/**
* 在返回值的前面声明泛型符号 在本方法中当成已知类型来用
* 在调用方法的时候 根据传递的实参 来确定类型
*
* @param t
* @param <T>
*/
public <T> T method1(T t) {
return t;
}
public <A> List<A> method2(A... t) {
List<A> list = new ArrayList<>();
return list;
}
public <A, B> void method3(A a, B b) {
}
/**
* 静态方法使用泛型符号
*
* @param a
* @param <A>
*/
public static <A> void method4(A a) {
}
}
测试类
public static void main(String[] args) {
List<Integer> integers = Arrays.asList(12, 55, 99, 10);
List<String> strings = Arrays.asList("java", "java2");
//传递实参的时候 确定类型
GenericMethod genericMethod = new GenericMethod();
genericMethod.method1("java");
genericMethod.method1(12);
List<String> strings1 = genericMethod.method2("java");
List<Integer> integers1 = genericMethod.method2(12, 11, 13);
}
1.4.泛型上下限
父类型
public abstract class Animal {
public abstract void eat();
}
子类型
public class Monkey extends Animal {
@Override
public void eat() {
System.out.println("猴子喜欢吃香蕉");
}
}
饲养员类型
public class Feeder {
/**
* ?:通配符 任意类型
* ? extends Animal:此类型既可以是Animal 也可以是Animal后代类型
* ? extends Animal:泛型的上限
*
* @param animal
*/
public void feed(List<? extends Animal> animal) {
for (Animal a : animal) {
a.eat();
}
}
/**
* ? super Animal:既可以是Animal类型 也可以是Animal的父类型
* ? super Animal :泛型的下限
*
* @param animal
*/
public void feed2(List<? super Animal> animal) {
for (Object a : animal) {
}
}
}
测试类
public static void main(String[] args) {
Feeder feeder = new Feeder();
/* Animal a = new Monkey();
a.eat();*/
List<Animal> animalList = new ArrayList<>();
animalList.add(new Monkey());
animalList.add(new Dog());
List<Dog> dogList = new ArrayList<>();
dogList.add(new Dog());
dogList.add(new Dog());
dogList.add(new Dog());
List<Monkey> monkeyList = new ArrayList<>();
feeder.feed(dogList);
feeder.feed(monkeyList);
feeder.feed(animalList);
}
456





