今日记录我的泛型使用,供后期查阅。
主要包含泛型类,泛型属性,泛型方法,静态方法中使用泛型。
public class GenericOperationResultRep<T> {private boolean success; // 是否操作成功。true,成功;false,不成功。private String errorMessage; // 错误信息private T t; // 额外数据public static <T> GenericOperationResultRep<T> create(boolean success,String errorMessage,T t) {GenericOperationResultRep<T> result = new GenericOperationResultRep<>();result.success = success;result.errorMessage = errorMessage;result.t = t;return result;}public static <T> GenericOperationResultRep<T> createSuccess() {GenericOperationResultRep<T> result = new GenericOperationResultRep<>();result.success = true;return result;}public static <T> GenericOperationResultRep<T> createSuccess(T tmpT) {GenericOperationResultRep<T> result = createSuccess();result.t = tmpT;return result;}public static <T> GenericOperationResultRep<T> createFailure() {GenericOperationResultRep<T> result = new GenericOperationResultRep<>();result.success = false;return result;}public static <T> GenericOperationResultRep<T> createFailure(String errorMessage) {GenericOperationResultRep<T> 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;} }
我是程序员娟娟,
致力将工作中遇到的问题和解决方案记录下来,
分享给更多需要的同行。
如果对你有帮助,不妨点个关注吧!