问题:有时候一个方法里面嵌套了很多逻辑,想拆分为多个方法方便调用;或者一个方法复用性很高,这时,这个方法嵌套在局部方法里面肯定是不方便的,如何快速抽取出这个方法?

public class Demo {
private static void getInfo(Object obj) {
Class<?> clazz = obj.getClass();
Method[] methods = clazz.getMethods();
for (Method method : methods) {
String name = method.getName();
Class<?> returnType = method.getReturnType();
Class<?>[] parameterTypes = method.getParameterTypes();
} //-----------------------------我即将抽取的-------------------------//
Field[] declaredFields = clazz.getDeclaredFields();
for (Field field : declaredFields) {
String name = field.getName();
Class c1 = field.getType();
String type = c1.getName();
}
//------------------------------我即将抽取的------------------------//
} }

选中我即将抽取的代码,按快捷键Ctrl + Alt + M 即可,或者 鼠标右击 》Refactor 》Extract 》Method 出现如下

抽取后自动生成代码如下,后续此方法就可以方便的被调用了

public class Demo {
private static void getInfo(Object obj) {
Class<?> clazz = obj.getClass();
Method[] methods = clazz.getMethods();
for (Method method : methods) {
String name = method.getName();
Class<?> returnType = method.getReturnType();
Class<?>[] parameterTypes = method.getParameterTypes();
} //-----------------------------我即将抽取的-------------------------//
commonDeal(clazz);
//------------------------------我即将抽取的------------------------//
} private static void commonDeal(Class<?> clazz) {
Field[] declaredFields = clazz.getDeclaredFields();
for (Field field : declaredFields) {
String name = field.getName();
Class c1 = field.getType();
String type = c1.getName();
}
} }

对应的还有变量的抽取、常量的抽取等,看下图,这是鼠标右击 》Refactor 》Extract 操作后出现的效果,里面包含很多的抽取:

最新文章

  1. WebDriver API元素的定位
  2. mysql 操作杂记
  3. 把crosswalk打包到cordova项目中
  4. angular 指令作用域 scope
  5. 数据库SQL语句学习--view
  6. 20145320 《Java程序设计》第10周学习总结
  7. Exception error message with incorrect line number
  8. socket设置为非阻塞方式(windows和linux)
  9. GCC常用参数
  10. 学习札记 ----wind7下如何安装SqlServer数据库
  11. webapi 跨域问题
  12. HDU 2586 How far way?
  13. BZOJ 3622
  14. Myschool乱码问题 和mysql 备份还原
  15. nginx下目录浏览及其验证功能、版本隐藏等配置记录
  16. 在dosbox窗口显示a~z
  17. centos7 下安装mysql 关键步骤
  18. mysql打开binlog
  19. ASP.NET MVC使用AuthenticationAttribute验证登录
  20. SpringBoot实现热加载方式

热门文章

  1. 解决修改JDK环境变量不生效方法
  2. Jupyter Notebook 更改本地目录
  3. 洛谷 P5569 [SDOI2008]石子合并 GarsiaWachs算法
  4. layout components pages及基本操作
  5. 题解【洛谷P1074】[NOIP2009]靶形数独
  6. 题解【洛谷P3662】[USACO17FEB]Why Did the Cow Cross the Road II S
  7. The entity type XXX is not part of the model for the current context.
  8. IntelliJ IDEA 2017.3尚硅谷-----自动导包
  9. 使用SQL命令行更改数据库字段类型
  10. [lua]紫猫lua教程-命令宝典-L1-01-01. Lua环境与IDE