首页 > 技术文章 > Java学习 泛型方式记录

guanxinjing 2020-01-14 21:09 原文

前言 

  Java泛型(generics)是JDK 5中引入的一个新特性,泛型提供了编译时类型安全监测机制,该机制允许程序员在编译时监测非法的类型。使用泛型机制编写的程序代码要比那些杂乱地使用Object变量,然后再进行强制类型转换的代码具有更好的安全性和可读性。泛型对于集合类尤其有用,例如,ArrayList就是一个无处不在的集合类。

  泛型的本质是参数化类型,也就是所操作的数据类型被指定为一个参数。

  因为泛型的样式很多,本人有时候经常忘记一些泛型的写法,所以特此弄一个博客记录一下。

 

泛型类

  单个泛型类

public class MyBean<T> {
    public T content;

    public T getContent() {
        return content;
    }

    public void setContent(T content) {
        this.content = content;
    }
}

  多个泛型类

public class MyBean<T,U> {
    public T content;
    public U name;

    public T getContent() {
        return content;
    }

    public void setContent(T content) {
        this.content = content;
    }

    public U getName() {
        return name;
    }

    public void setName(U name) {
        this.name = name;
    }
}

  单个泛型继承抽象类

public class MyView<T extends View> {
    public View content;

    public T getContent() {
        return (T)content;
    }

    public void setContent(T content) {
        this.content = content;
    }

}

  多个泛型继承抽象类

public class MyBean<T,U extends View> {
    public T content;
    public U view;

    public T getContent() {
        return content;
    }

    public void setContent(T content) {
        this.content = content;
    }

    public U getView() {
        return view;
    }

    public void setView(U view) {
        this.view = view;
    }
}

  接口泛型类

public class MyBean<E> implements Bean<E> {
    
    @Override
    public <E1> E1 show() {
        return null;
    }
}

泛型方法

形参

  类带泛型的形参

public class MyUtil<T> {

    public void get(T t) {
        
    }
}

  形参的类带泛型

public class MyUtil<T>{

    public void get(Class<T> t) {

    }
}

  类不带泛型的形参

  必需在返回值里带泛型

public class MyUtil {

    public <T> T get(T t) {
        return t;
    }
}

  数组形参泛型

 public <T> T[] get(T[] ts){
        return ts;
    }

  类型通配符

 

public void getData(List<?> ts){

}

 

返回值

  返回泛型

public class MyUtil {
    public <T> T getContent() {
        return (T)new String("demo");
    }
}

  返回类里的泛型

public class MyBean<T> {
    private T content;

    public T getContent() {
        return content;
    }
}

  泛型也可以是一个集合

  这个有点意义不明,就只是想表达一下List<String> 也可以是一个 T

public class MyUtil<T> {
    private T content;

    public List<String> getContent() {
        return (List<String>) content;
    }
}

  返回一个带泛型的类

public class MyUtil<T> {

    public Class<T> getContent(Class c){
        return c;
    }

}

  返回一个泛型继承的类

public class MyUtil {

    public static <T extends View> T getContent(Context context) {
        return (T)new View(context);
    }
}

  返回通配符的类

    @Nullable
    private static Class<? extends ViewBinding> findViewBinding(@NonNull Object object) {
        Type type = object.getClass().getGenericSuperclass();
        if (type == null) {
            return null;
        }
        Type[] types = ((ParameterizedType) type).getActualTypeArguments();
        if (types.length == 0) {
            return null;
        }
        try {
            return (Class<? extends ViewBinding>) types[0];
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

 

 

 

 

泛型接口类

  单个泛型接口

public interface Listener<E> {
    <E> E show();
}

  多个泛型接口

public interface Listener<E,R> {
    <E> E showE();
    <R> R showR();
}

  单个泛型继承抽象

public interface Listener<E extends View> {
    <E extends View> E show();
}

  接口方法返回泛型

  跟上面的泛型方法一样,不多重复说明,只是告诉你接口类的方法可以返回泛型或者带泛型形参(抽象类也是)

public interface Listener {
    <T> T get(T t);
}

End

推荐阅读