Java泛型—Java语法糖,只在编译有作用,编译后擦出泛型
在代码进入和离开的边界处,会处理泛型
Java泛型作用:在编译时进行语法检查。
泛形:泛型类,泛型接口,泛型方法
泛型类:
@Data
public class GenericClassExample<T> {
//由外部指定
private T member;
public GenericClassExample(T member){
this.member = member;
}
public T handleSomething(T target){
return target;
}
}
public class GenericDemo {
public static void main(String args[]) {
GenericClassExample<String> example = new GenericClassExample<>("Hello");
System.out.println(example.handleSomething("Hi").getClass());
GenericClassExample<Integer> example1 = new GenericClassExample<>(123);
System.out.println(example1.handleSomething(11).getClass());
}
}
执行结果:
class java.lang.String
class java.lang.Integer
如下Java泛型不支持继承,但是可以使用通配符支持泛型继承。
@Data
public class GenericClassExample<T> {
//由外部指定
private T member;
public GenericClassExample(T member){
this.member = member;
}
public T handleSomething(T target){
return target;
}
}
public class GenericDemo {
public static void handlerMember(GenericClassExample<Number> genericClassExample){
Integer result = 1;
System.out.println(result+(Integer) genericClassExample.getMember());
}
public static void main(String args[]) {
handlerMember(new GenericClassExample<Number>(12));
//编译报错
handlerMember(new GenericClassExample<Integer>(12));
}
}
但是可以使用通配符支持泛型继承,添加上界,下届;
?通配符和Object有点类似,没有达到编译预检查的效果所以和泛型目的相悖。
所以使用上界和下界实现继承。
使用上界 <? extends Number>—泛型实参的父类是Number
public class GenericDemo {
public static void handlerMember(GenericClassExample<? extends Number> genericClassExample){
Integer result = 1;
System.out.println(result+(Integer) genericClassExample.getMember());
}
public static void main(String args[]) {
//编译未报错
handlerMember(new GenericClassExample<Integer>(12));
handlerMember(new GenericClassExample<Number>(12));
}
}
使用上界 <? super Integer>泛型实参的子类是Integer
public class GenericDemo {
public static void handlerMember(GenericClassExample<? super Integer> genericClassExample){
Integer result = 1;
System.out.println(result+(Integer) genericClassExample.getMember());
}
public static void main(String args[]) {
//编译未报错
handlerMember(new GenericClassExample<Integer>(12));
handlerMember(new GenericClassExample<Number>(12));
}
}
泛型接口:
public interface GenericIFactory<T, N> {
T nextObject();
N nextNumber();
}
public class RobotFactory implements GenericIFactory<String, Integer> {
private String[] ss = new String[]{"Hello", "Hi"};
private Integer[] dd = new Integer[]{123, 000};
@Override
public String nextObject() {
Random ro = new Random();
return ss[ro.nextInt(2)];
}
@Override
public Integer nextNumber() {
Random ro = new Random();
return dd[ro.nextInt(2)];
}
public static void main(String args[]){
GenericIFactory<String,Integer> genericIFactory = new RobotFactory();
System.out.println(genericIFactory.nextNumber());;
System.out.println(genericIFactory.nextObject());;
}
}
泛型接口实现类
public class GenericFactoryImpl<T,N> implements GenericIFactory<T,N> {
@Override
public T nextObject() {
return null;
}
@Override
public N nextNumber() {
return null;
}
}
泛型方法
@Data
public class GenericClassExample<T> {
//由外部指定
private T member;
//构造函数
public GenericClassExample(T member) {
this.member = member;
}
//类中的普通成员方法
public T handleSomething(T target) {
return target;
}
//泛型方法
public static <E> void printArray(E[] array) {
for (E e : array) {
System.out.printf("%s", e);
System.out.printf(" ");
}
}
}
E Element 元素,在集合中使用
T Type 在泛型类中使用
K Key
V Value
N Number
R. Result
网友评论