zoukankan      html  css  js  c++  java
  • 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. }  

    选中我即将抽取的代码,按快捷键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. }  

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


  • 相关阅读:
    codeforce 896A
    CQH分治与整体二分
    [CQOI2011]动态逆序对
    codeforce Hello 2018 913F sol
    A*算法[k短路([SDOI2010]魔法猪学院)]
    bzoj3524 [POI2014]Couriers
    整体二分
    bzoj5016 [SNOI2017]一个简单的询问
    CF176E Archaeology
    bzoj4551 [TJOI2016&HEOI2016]树
  • 原文地址:https://www.cnblogs.com/jpfss/p/9224770.html
Copyright © 2011-2022 走看看