构造器和泛型的好处

开发中,经常有标准的返回实体

/**
* 状态(success:成功, fail:失败)
*/
protected String status = GlobalConstants.FAIL;

/**
* 消息
*/
protected String message;

/**
* fail时,具体错误码
*/
protected String errorCode;

/**
* 业务code
* */
protected String code;

/**
* 详细数据
*/

public T result;

这个时候利用构造函数,当返回实体中只有一个result的时候,利用构造函数可以直接

return new BaseResponse<>(response);

public BaseResponse(T result) {
this.status = GlobalConstants.SUCCESS;
this.result = result;
}

避免了大量的写set

猜你喜欢

转载自www.cnblogs.com/woainixxx/p/10878706.html