java泛型:泛型类,泛型方法

今日记录我的泛型使用,供后期查阅。

主要包含泛型类,泛型属性,泛型方法,静态方法中使用泛型。

public class GenericOperationResultRep {
    private boolean success; // 是否操作成功。true,成功;false,不成功。
    private String errorMessage; // 错误信息
    private T t; // 额外数据
    

    public static  GenericOperationResultRep create(boolean success,
                                                          String errorMessage,
                                                          T t) {

        GenericOperationResultRep result = new GenericOperationResultRep<>();

        result.success = success;
        result.errorMessage = errorMessage;
        result.t = t;

        return result;
    }

    public static  GenericOperationResultRep createSuccess() {
        GenericOperationResultRep result = new GenericOperationResultRep<>();
        result.success = true;
        return result;
    }

    public static  GenericOperationResultRep createSuccess(T tmpT) {
        GenericOperationResultRep result = createSuccess();

        result.t = tmpT;

        return result;
    }

    public static  GenericOperationResultRep createFailure() {
        GenericOperationResultRep result = new GenericOperationResultRep<>();
        result.success = false;
        return result;
    }

    public static  GenericOperationResultRep createFailure(String errorMessage) {
        GenericOperationResultRep result = createFailure();

        result.errorMessage = errorMessage;

        return result;
    }

    //

    // 是否失败
    public boolean fail(){
        return !success;
    }

    // 是否成功
    public boolean success() {
        return success;
    }

    //

    public boolean getSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public T getT() {
        return t;
    }

    public void setT(T t) {
        this.t = t;
    }
}


我是程序员娟娟,

致力将工作中遇到的问题和解决方案记录下来,

分享给更多需要的同行。

如果对你有帮助,不妨点个关注吧!

你可能感兴趣的:(java,java,开发语言)