intellij idea中快速抽取方法

Intellij Idea使用教程汇总篇

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

  1. public class Demo {  
  2.     private static void getInfo(Object obj) {  
  3.         Class<?> clazz = obj.getClass();  
  4.         Method[] methods = clazz.getMethods();  
  5.         for (Method method : methods) {  
  6.             String name = method.getName();  
  7.             Class<?> returnType = method.getReturnType();  
  8.             Class<?>[] parameterTypes = method.getParameterTypes();  
  9.         }  
  10.   
  11.         //-----------------------------我即将抽取的-------------------------//  
  12.         Field[] declaredFields = clazz.getDeclaredFields();  
  13.         for (Field field : declaredFields) {  
  14.             String name = field.getName();  
  15.             Class c1 = field.getType();  
  16.             String type = c1.getName();  
  17.         }  
  18.         //------------------------------我即将抽取的------------------------//  
  19.     }  
  20.   
  21. }  
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 出现如下

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

  1. public class Demo {  
  2.     private static void getInfo(Object obj) {  
  3.         Class<?> clazz = obj.getClass();  
  4.         Method[] methods = clazz.getMethods();  
  5.         for (Method method : methods) {  
  6.             String name = method.getName();  
  7.             Class<?> returnType = method.getReturnType();  
  8.             Class<?>[] parameterTypes = method.getParameterTypes();  
  9.         }  
  10.   
  11.         //-----------------------------我即将抽取的-------------------------//  
  12.         commonDeal(clazz);  
  13.         //------------------------------我即将抽取的------------------------//  
  14.     }  
  15.   
  16.     private static void commonDeal(Class<?> clazz) {  
  17.         Field[] declaredFields = clazz.getDeclaredFields();  
  18.         for (Field field : declaredFields) {  
  19.             String name = field.getName();  
  20.             Class c1 = field.getType();  
  21.             String type = c1.getName();  
  22.         }  
  23.     }  
  24.   
  25. }  
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 操作后出现的效果,里面包含很多的抽取:


转载于:https://www.cnblogs.com/jpfss/p/9224770.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值