Java return 问题
--------------------编程问答-------------------- 我也觉得很奇怪。。。 --------------------编程问答-------------------- 递归? --------------------编程问答-------------------- 这不是传说中的递归嘛~ --------------------编程问答-------------------- 递归啊。。 --------------------编程问答-------------------- public static String processContentCustomer(String smsContent,List<SmsSignCustomerDto> dtoList,String customerId){
public static String processContentCustomer(String smsContent,List<SmsSignCustomerDto> dtoList,String customerId){
if(pattern.matcher(smsContent).find()){
//return 后直接返回到这里.
processContentCustomer(smsContent,dtoList,customerId);
}else{
//进入这里并没有返回到方法的顶部 ,而是上面的那个的这个方法是怎么回事?
return smsContent;
}
}
if(pattern.matcher(smsContent).find()){
//return 后直接返回到这里.
processContentCustomer(smsContent,dtoList,customerId);
}else{
//进入这里并没有返回到方法的顶部 ,而是上面的那个的这个方法是怎么回事?
return smsContent;
}
return smsContent;
}
--------------------编程问答--------------------
/**
* 处理客户标记
* @param smsContent
* @param dtoList
* @param customerId
* @return
*/
public static String processContentCustomer(String smsContent,List<SmsSignCustomerDto> dtoList,String customerId){
String regex = "\\{(\\w)*\\}";
//得到SmsSignUserDto 的所有属性
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(smsContent);
if (matcher.find()) {
for(SmsSignCustomerDto dto:dtoList){
String replace = smsContent.substring(matcher.start(), matcher.end());
String sign = replace.substring(1, replace.length()-1);
String stringLetter = sign.substring(0, 1).toUpperCase();
// 获得相应属性的getXXX和setXXX方法名称
String getName = "get" + stringLetter + sign.substring(1);
if(customerId == null){
return smsContent;
}
if(!customerId.equals(dto.getCustomerCode())){
continue;
}
Method method;
try {
method = dto.getClass().getDeclaredMethod(getName, null);
Object result = method.invoke(dto, null);
String rs = smsContent.replace(replace, result.toString());
smsContent = rs;
} catch (SecurityException e) {
return smsContent;
} catch (NoSuchMethodException e) {
return smsContent;
} catch (IllegalArgumentException e) {
return smsContent;
} catch (IllegalAccessException e) {
return smsContent;
} catch (InvocationTargetException e) {
return smsContent;
}
if(pattern.matcher(smsContent).find()){
processContentCustomer(smsContent,dtoList,customerId);
}else{
return smsContent;
}
}
}
return smsContent;
}
完整的方法是这样的. --------------------编程问答-------------------- 我怎么在
--------------------编程问答--------------------
if(pattern.matcher(smsContent).find()){
processContentCustomer(smsContent,dtoList,customerId);
}else{
//这里返回这个方法
return smsContent;
}
public static String processContentCustomer(String smsContent,List<SmsSignCustomerDto> dtoList,String customerId){
if(pattern.matcher(smsContent).find()){
//我个人认为:这个是一个个的匹配文件,找到了,就继续递归调用该方法processContentCustomer,直到没有文件,则直接返回smsContent
//return 后直接返回到这里.
processContentCustomer(smsContent,dtoList,customerId);
}else{
//进入这里并没有返回到方法的顶部 ,而是上面的那个的这个方法是怎么回事?
return smsContent;
}
}
就是一个递归的调用啊,亲…… --------------------编程问答--------------------
要返回 什么方法…… 这个方法是做什么的哈????你能说清楚吗??? --------------------编程问答--------------------
我知道是什么原因, 就是找不到好的解决方案.你看下后面的2个完整的代码截图. --------------------编程问答--------------------
返回到最初进入时的这个方法, 然后返回. 描述能不行啊. 求知音.... --------------------编程问答-------------------- 怎么没人关注啊, 这么经典的问题. --------------------编程问答--------------------
你想要什么,表述清楚 --------------------编程问答--------------------
我就是想把 一条像这样的内容:尊敬的{XX}你的卡上余额为{XX} 把{XX}全部替换成 数据库中取出的数据
参数 smsContent 为这天内容,List<SmsSignCustomerDto> dtoList 为用存储过程查询出来管理要修改{XX}的数据, customerId 为要和 dtoList 某一条数据的ID 相同, 才说明 dtoList 里面的数据是属于 这个customer的. 描述能力不行, 请谅解. --------------------编程问答--------------------
我再仔细看了我的回复, 汗颜啊.... --------------------编程问答-------------------- 你这不会报错奇迹
--------------------编程问答-------------------- ⊙﹏⊙b汗……
public static String processContentCustomer(String smsContent,List<SmsSignCustomerDto> dtoList,String customerId){
if(pattern.matcher(smsContent).find()){
//return 后直接返回到这里.
return processContentCustomer(smsContent,dtoList,customerId); 应该这样
}else{
//进入这里并没有返回到方法的顶部 ,而是上面的那个的这个方法是怎么回事?
return smsContent;
}
}
寡人 给 跪了……………… --------------------编程问答-------------------- 能重新仔细描述 你要问的问题吗???
什么返回 最初的方法……
之前不是又一次递归了吗??? 我勒个去…… --------------------编程问答--------------------
我想在第二个方法怎么跳回到第一个方法返回.
补充:Java , Java SE