Java泛型:利⽤泛型动态确认⽅法返回值类型
根据泛型类型动态返回对象
public <T extends PackObject> T unPackMessage(String interfaceCode, String respValue, Class<T> clazz, String sysType) throws IOException {
log.info(">> unPackMessage start, interfaceCode is {}, respValue is {}", interfaceCode, respValue);
Map<String, Object> result = null;
// get pack_convert.prop file content
人物访谈PackConvertMessage packConvertMessage = getPackConvertFileContent();
String func = InterfaceMapping().get(interfaceCode);
if (StringUtils.isBlank(func)){
<("<< unPackMessage error, No respon message with current interface configured");
return null;
}
String respSerialNumber = RespFuncMapping().get(func);
String currPackType = PackConvertConstant.SystermType.CLIENT.equals(sysType) ? client_pack_type : pack_type;
switch (currPackType){
ca PackConvertConstant.PackType.NON_FIXED_LENGTH:
result = unpackNoFixedMessage(respValue, connect_char, respSerialNumber, PACK_CONVERT_FILE_MAPPING_SEPARATOR, sysType);
break;
ca PackConvertConstant.PackType.FIXED_LENGTH:
result = unpackFixedMessage(respValue, respSerialNumber, PACK_CONVERT_FILE_MAPPING_SEPARATOR);
break;
ca PackConvertConstant.PackType.XML:
result = unpackXmlMessage(respValue, respSerialNumber, PACK_CONVERT_FILE_MAPPING_SEPARATOR);
break;
ca PackConvertConstant.PackType.JSON:
result = unpackJsonMessage(respValue, respSerialNumber);
break;
}
流氓兔图片
log.info("<< unPackMessage success, return value is {}", JSONString(result));
T t = JavaObject(JSON.JSONString(result)), clazz);
return t;
}
根据泛型类型动态返回集合
public <E extends BaLiquidation, T extends BaLiquidation> List<E> convertFileToObj(String localFilePath, Class<E> clazz, Class<T> tclazz) throws IOException, NoSuchMethodException, IllegalAccessException, InstantiationException {
logger.info(">> convertFileToObj, localFilePath is {}, clazz is {}", localFilePath, Name());
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(localFilePath),"utf-8"));解构主义
StringBuilder sb = new StringBuilder();
String line;血栓的治疗方法
while ((line = br.readLine()) != null){
sb.append(line);
sb.Property("line.parator"));
}
List<E> result = String(), clazz);
logger.info("<< convertFileToObj end");
return result;
}
痉挛性便秘public <E extends BaLiquidation> List<E> convertFileToObj(String localFilePath, Class<E> clazz) throws IOException, NoSuchMethodException, IllegalAccessException, InstantiationException {
logger.info(">> convertFileToObj, localFilePath is {}, clazz is {}", localFilePath, Name());
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(localFilePath),"utf-8"));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null){
鱼的简笔画图片大全劳工标准sb.append(line);
sb.Property("line.parator"));
}
List<E> result = String(), clazz);
logger.info("<< convertFileToObj end");
return result;
}
泛型⽅法基本介绍
public与返回值中间的<T>⾮常重要,表⽰此⽅法声明为泛型⽅法
只有声明了<T>的⽅法才是泛型⽅法,泛型类中使⽤了泛型的成员⽅法并不能算是泛型⽅法
各种职业的英文
<T>表明该⽅法将使⽤泛型类型T,此时才可以在⽅法中使⽤泛型类型T
与泛型类的定义⼀样,T可以随便写成任意标识,⽐如T、E、K、V、S等
泛型限制上下边界
泛型限制上边界:传⼊的类型只能是指定类的⼦类,⽐如Class<? extend PackObject>
泛型限制下边界:传⼊的类型只能是指定类的⽗类,⽐如DiagnosticListener<? super JavaFileObject>
另外附常⽤泛型标识说明
常见的⼀些泛型标识就是T(类型)、E(元素)、K(key)、V(value)、?(通配符,不确定的)这⼏个,其实填任何字母都⾏