优化这段代码boolean flag = permissionService.isPermitted("system:course:list"); if (flag) { return listAll(vo); } flag = permissionService.isPermitted("system:course:list:dept"); if (flag) { return listByDept(vo); } flag = permissionService.isPermitted("system:course:list:creator"); if (flag) { return listByCreator(vo); } return R.failed("对不起,您没有课程查询权限");
时间: 2024-03-13 16:46:27 浏览: 153
您可以尝试使用switch-case语句来优化这段代码,减少重复的判断:
```
String permission = "";
if(permissionService.isPermitted("system:course:list")){
permission = "listAll";
} else if(permissionService.isPermitted("system:course:list:dept")){
permission = "listByDept";
} else if(permissionService.isPermitted("system:course:list:creator")){
permission = "listByCreator";
} else {
return R.failed("对不起,您没有课程查询权限");
}
try {
Method method = this.getClass().getMethod(permission, vo.getClass());
return (R) method.invoke(this, vo);
} catch (Exception e) {
e.printStackTrace();
return R.failed("系统异常,请联系管理员");
}
```
这样可以将重复的代码进行封装,提高了代码的可读性和可维护性。
阅读全文
相关推荐

















