zoukankan      html  css  js  c++  java
  • idea抽取方法

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

    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 操作后出现的效果,里面包含很多的抽取:

  • 相关阅读:
    线段树&&线段树的创建线段树的查询&&单节点更新&&区间更新
    树&二叉树&&满二叉树&&完全二叉树&&完满二叉树
    Git学习记录 力做全网最强入门教程
    Markdown测试
    [转载] c++对结构体数组排序
    c/c++ 中#ifndef和#endif的作用及使用
    交互题(apio2016Gap)
    linux下对拍
    CTSC2017密钥、吉夫特
    省队十连测
  • 原文地址:https://www.cnblogs.com/eternityz/p/12239742.html
Copyright © 2011-2022 走看看