给定class对象返回该类的实例
今天分享给大家一个实用的开发技巧, 创建一个返回值为泛型的对象构建函数, 要求是传入返回值类的 class 对象.
例如: 平时我们开发接口的时候发现很多响应类里面基本都会有`code`和`error`两个属性, 而且有的人会将常量的枚举值作为接口调用成功的返回值, 这时候每次都会在接口的末尾处 new 一个接口响应值返回给调用者, 而这时候我们可以统一将这个创建含有成功响应值对象封装为一个方法.
为了节省更多重复代码, 我们会将封装这个方法定义在一个父类中.
父类代码改造前代码:
/**
* @Title: BaseResponse
* @Description:
* @author: deWei
* @date: 2022/3/12 18:28
* @Version: 1.0
*/
@Data
@NoArgsConstructor
public class BaseResponse implements Serializable {
<p style="text-indent:2em"/>private static final long serialVersionUID = -8383361046902228836L;
<p style="text-indent:2em"/>protected String code;
<p style="text-indent:2em"/>protected String errorMsg;
}
思路: ①由于我们不能创建一个对象传入而返回, 这样就无法达到我们减少代码量的需求了, 所以就要用到 java 的反射机制去构建对象, 最后得到参数为 class 对象. ②既然是在父类中定义, 那构建的对象必须就是它的子类, 这时候就需要用到泛型去限制了.
ok, 整理好思路了就开始改造父类了.
父类代码改造后代码 :
/**
* @Title: BaseResponse
* @Description:
* @author: deWei
* @date: 2022/3/12 18:28
* @Version: 1.0
*/
@Data
@NoArgsConstructor
public class BaseResponse implements Serializable {
<p style="text-indent:2em"/>private static final long serialVersionUID = -8383361046902228836L;
<p style="text-indent:2em"/>protected String code;
<p style="text-indent:2em"/>protected String errorMsg;
<p style="text-indent:2em"/>public static <T extends BaseResponse> T setResult(Class<T> tClass, ResponseEnum responseEnum) throws Exception {
<p style="text-indent:2em"/><p style="text-indent:2em"/>Constructor<T> constructor = tClass.getConstructor(ResponseEnum.class);
<p style="text-indent:2em"/><p style="text-indent:2em"/>return constructor.newInstance(responseEnum);
<p style="text-indent:2em"/>}
<p style="text-indent:2em"/>/**
<p style="text-indent:2em"/> * 调用的子类需要重写父类的 BaseResponse(ResponseEnum responseEnum) 构造器
<p style="text-indent:2em"/> */
<p style="text-indent:2em"/>public static <T extends BaseResponse> T success(Class<T> tClass) {
<p style="text-indent:2em"/><p style="text-indent:2em"/>try {
<p style="text-indent:2em"/><p style="text-indent:2em"/><p style="text-indent:2em"/>return setResult(tClass, ResponseEnum.NORMAL_SUCCESS);
<p style="text-indent:2em"/><p style="text-indent:2em"/>} catch (Exception e) {
<p style="text-indent:2em"/><p style="text-indent:2em"/><p style="text-indent:2em"/>e.printStackTrace();
<p style="text-indent:2em"/><p style="text-indent:2em"/>}
<p style="text-indent:2em"/><p style="text-indent:2em"/>return null;
<p style="text-indent:2em"/>}
<p style="text-indent:2em"/>public static <T extends BaseResponse> T failure(Class<T> tClass) {
<p style="text-indent:2em"/><p style="text-indent:2em"/>try {
<p style="text-indent:2em"/><p style="text-indent:2em"/><p style="text-indent:2em"/>return setResult(tClass, ResponseEnum.SYSTEM_ERROR);
<p style="text-indent:2em"/><p style="text-indent:2em"/>} catch (Exception e) {
<p style="text-indent:2em"/><p style="text-indent:2em"/><p style="text-indent:2em"/>e.printStackTrace();
<p style="text-indent:2em"/><p style="text-indent:2em"/>}
<p style="text-indent:2em"/><p style="text-indent:2em"/>return null;
<p style="text-indent:2em"/>}
<p style="text-indent:2em"/>public BaseResponse(ResponseEnum responseEnum) {
<p style="text-indent:2em"/><p style="text-indent:2em"/>this.code = responseEnum.code();
<p style="text-indent:2em"/><p style="text-indent:2em"/>this.errorMsg = responseEnum.errorMsg();
<p style="text-indent:2em"/>}
}
响应枚举 ResponseEnum.java:
/**
* @Title: ResponseEnum
* @Description: 响应类实体
* @author: deWei
* @date: 2022/3/12 18:28
* @Version: 1.0
*/
public enum ResponseEnum {
<p style="text-indent:2em"/>NORMAL_SUCCESS("000000", "成功"),
<p style="text-indent:2em"/>SYSTEM_ERROR("000001", "系统异常");
<p style="text-indent:2em"/>private final String code;
<p style="text-indent:2em"/>private final String errorMsg;
<p style="text-indent:2em"/>ResponseEnum(String code, String errorMsg) {
<p style="text-indent:2em"/><p style="text-indent:2em"/>this.code = code;
<p style="text-indent:2em"/><p style="text-indent:2em"/>this.errorMsg = errorMsg;
<p style="text-indent:2em"/>}
<p style="text-indent:2em"/>public String code() {
<p style="text-indent:2em"/><p style="text-indent:2em"/>return code;
<p style="text-indent:2em"/>}
<p style="text-indent:2em"/>public String errorMsg() {
<p style="text-indent:2em"/><p style="text-indent:2em"/>return errorMsg;
<p style="text-indent:2em"/>}
}
给定class对象返回该类的实例的更多相关文章
- Java中反射机制和Class.forName、实例对象.class(属性)、实例对象getClass()的区别
一.Java的反射机制 每个Java程序执行前都必须经过编译.加载.连接.和初始化这几个阶段,后三个阶段如下图: 其中
- C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值
转自goldeneyezhang原文 C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值 C#利用反射,遍历获得一个类的所有属性名,以及该类的实例的所有属性的值总结: 对应某个类的 ...
- 本文使用springMVC和ajax,实现将JSON对象返回到页面
一.引言 本文使用springMVC和ajax做的一个小小的demo,实现将JSON对象返回到页面,没有什么技术含量,纯粹是因为最近项目中引入了springMVC框架. 二.入门例子 ①. 建立工程, ...
- C++11用于计算函数对象返回类型的统一方法
[C++11用于计算函数对象返回类型的统一方法] 模板 std::result_of 被TR1 引进且被 C++11 所采纳,可允许我们决定和使用一个仿函数其回返值的类别.底下,CalculusVer ...
- JavaScript中创建字典对象(dictionary)实例
这篇文章主要介绍了JavaScript中创建字典对象(dictionary)实例,本文直接给出了实现的源码,并给出了使用示例,需要的朋友可以参考下 对于JavaScript来说,其自身的Array对象 ...
- php xml格式对象 返回->对应格式数组
/* * $objXml xml格式对象 * 返回 : 对应格式数组 */ public function XmlString2Arr($xml) { ...
- easyUIDataGrid对象返回值
import java.util.List; /** * easyUIDataGrid对象返回值 * <p>Title: EasyUIResult</p> * <p> ...
- JS window对象 返回前一个浏览的页面 back()方法
JS window对象 返回前一个浏览的页面 back()方法,加载 history 列表中的前一个 URL. 语法: window.history.back(); 返回前一个浏览的页面 back ...
- valueOf()对象返回值
valueOf()对象返回值 Array数组的元素被转换为字符串,这些字符串由逗号分隔,连接在一起.其操作与 Array.toString 和 Array.join 方法相同. Boolean为Boo ...
随机推荐
- JAVA多线程提高十四:同步工具Exchanger
Exchanger可以在对中对元素进行配对和交换的线程的同步点.每个线程将条目上的某个方法呈现给 exchange 方法,与伙伴线程进行匹配,并且在返回时接收其伙伴的对象.Exchanger 可能被视 ...
- HTC组件介绍及应用 HTML
转载请注明来源:https://www.cnblogs.com/hookjc/ HTML组件封装了HTML内容,并可以插入到别的HTML文档中.在HTML组件出现以前,在HMTL文档中使用自定义控制唯 ...
- 有关 iOS 的开发证书、应用标识、设备标识、配置文件以及密钥 #DF
iOS开发过程中如果需要进行真机调试.发布需要注册申请很多证书, 以下是对iOS开发的常用证书和密钥等的逐一简单说明: 证书 iOS常用的证书包括开发证书和发布证书,无论是真机调试还是最终发布应用到A ...
- 利用redis+AOP简单处理MQ冥等问题
思路: 1.利用redis内部的串行执行特性,使用getandset()处理分布式问题; 2.注解提供入参选择,通过数据抽取后计算MD5值,实现业务性值的冥等: 代码区: 1.注解 1 /** 2 * ...
- JabRef
# JabRef 下载 https://www.fosshub.com/JabRef.html # JabRef 安装 自己更改下目录直接安装接可以了 # 新建自己的库, 然后点击保存就可以了. # ...
- Solution -「ARC 082D」Sandglass
\(\mathcal{Description}\) Link. 一个沙漏内共 \(Xg\) 沙,令初始时上半部分为 A,下半部分为 B.沙漏在 \(r_1,r_2,\cdots,r_n\) 时 ...
- [LeetCode]28.实现strStr()(Java)
原题地址: implement-strstr 题目描述: 实现 strStr() 函数. 给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字 ...
- Spring Boot自动配置实战
上篇讲述了Spring Boot自动配置的原理,本篇内容就是关于该核心原理的实际应用.需求即当某个类存在的时候,自动配置这个类的bean并且这个bean的属性可以通过application.prope ...
- vscode使用vue代码模板
1.vscode中打开:文件>首选项>用户片段>vue.json 2.复制下面代码并保存. 点击查看代码 { "Print to console": { &quo ...
- 不需要高价购买BI工具,掌握这个Excel插件就能碾压大数据
曾几何时,EXCEL在数据界可谓是独树一帜,引领风骚,在职场中无人不知,无人不晓.但是随着大数据的出现,EXCEL便风光不再,江河日下,一度被其他大数据工具挤到了后面,逐渐被边缘化了.而我是一个EX ...