定义含有泛型的接口
- 定义GenericInterface接口
package com.Generic;
/*
定义含有泛型的接口
*/
public interface GenericInterface<I> {
public abstract void method(I i);
}
含有泛型的接口,第一种使用方式:
- 定义接口的实现类,实现接口,指定接口的类型
public interface Iterator<E>{
E next();
}
- Scanner类实现了Iterator接口,并指定接口的泛型为String,所有重写的next方法泛型默认就是String
public final class Scanner implements Iterator<String>{
public String next() {}
}
package com.Generic;
/*
含有泛型的接口,第一种使用方式:定义接口的实现类,实现接口,指定接口的类型
public interface Iterator<E>{
E next();
}
Scanner类实现了Iterator接口,并指定接口的泛型为String,所有重写的next方法泛型默认就是String
public final class Scanner implements Iterator<String>{
public String next() {}
}
*/
public class GenericInterfaceImpl1 implements GenericInterface<String>{
@Override
public void method(String s) {
System.out.println(s );
}
}
含有泛型的接口,第二种使用方式:
-
接口使用什么泛型,实现类就使用什么泛型,类跟着接口走
-
就相当于定义了一个含有泛型的类,创建对象的时候确定泛型的类型
public interface List<E>{
boolean add<E e>;
E get(int index);
}
public class ArrayList<E> implements List<E>{
public boolean add<E e>{}
public E get(int index){}
}
package com.Generic;
/*
含有泛型的接口的第二种使用方式:接口使用什么泛型,实现类就使用什么泛型,类跟着接口走
就相当于定义了一个含有泛型的类,创建对象的时候确定泛型的类型
public interface List<E>{
boolean add<E e>;
E get(int index);
}
public class ArrayList<E> implements List<E>{
public boolean add<E e>{}
public E get(int index){}
}
*/
public class GenericInterfaceImpl2<I> implements GenericInterface<I>{
@Override
public void method(I i) {
System.out.println(i);
}
}
测试含有泛型的接口
package com.Generic;
/*
测试含有泛型的接口
*/
public class Demo04GenericInterface {
public static void main(String[] args) {
//创建GenericInterfaceImpl1对象
GenericInterfaceImpl1 gi1 = new GenericInterfaceImpl1();
gi1.method("add");//add
//创建GenericInterfaceImpl2对象
GenericInterfaceImpl2<Integer> gi2 = new GenericInterfaceImpl2();
gi2.method(10);//10
GenericInterfaceImpl2<Double> gi3 = new GenericInterfaceImpl2<>();
gi3.method(2.3);//2.3
}
}