静态方法和泛型

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/m18870420619/article/details/79929316
public class ResponseBean<T> {
	
    private int code;
    private String message;
    private String lang;
    private String accessToken;

    private String sign;
    private int source;
    private Date time;
    private T value;

    public ResponseBean() {

    }

    private ResponseBean(int code, T value) {
        this(code, value, "");
    }

    private ResponseBean(int code, T value, String message) {
        this.time = new Date();
        this.lang = "zh_Cn";
        this.source = 201;
        this.code = code;
        this.value = value;
        this.message = message;
        
    }
    
    private ResponseBean(int code, String message) {
        this.time = new Date();
        this.lang = "zh_Cn";
        this.source = 201;
        this.code = code;
        this.message = message;
    }

    public static <T> ResponseBean<T> error(String msg,  T data) {
        return new ResponseBean<>(200, data, msg);
    }

    省略get()set()方法
}

静态方法不能访问类中定义的泛型,如果静态方法中数据类型不确定,可以在方法上定义泛型。

即上例中<T>就是声名一个泛型T,之后两个T是在使用泛型T。

猜你喜欢

转载自blog.csdn.net/m18870420619/article/details/79929316