IntelliJ IDEA 2020.3使用教程:重构代码的3种方法

IntelliJ IDEA 2020.3提供了许多实用的功能,例如调试时的交互式提示,Git暂存支持,对Java 15记录和密封类的扩展支持等等。它简化了端点,框架和事件探查器的平常工做。经过基于机器学习技术的更好的代码完成,更直观和有用的新的“Welcome”屏幕以及更好的拼写和语法检查,整个UX获得了改进。简而言之,一切都更好!(本文主要讲述“提取”这种方法,点击查看完整版!)html

下载IntelliJ IDEA 2020.3框架

这篇博客文章涵盖了与视频相同的内容,并包含一些其余提示和技巧。点击查看视频>>机器学习

简化代码具备不少优点,包括提升可读性,解决技术难题以及管理不断变化的需求。咱们将在此博客中介绍三种重构类型:ide

  • 提取和内联
  • 更改签名
  • 重命名

提取和内联学习

简化代码的第一种方法是提取它。您能够在IntelliJ IDEA中执行五种类型的提取重构:url

  • 提取方法
  • 提取常数
  • 提取字段
  • 提取变量
  • 提取参数

提取方法.net

此方法中的switch语句与该方法的其他部分不一致。调试

public class PlanetExtractions {

    Planet myPlanet = new Planet("earth");

    // I'm using PlanetExtractions to get the facts for my country
    // I'm using planetextractions to get the facts for my country
    private void printPlanetFacts(final String country) {
        System.out.println("Planet name is " + myPlanet.getName());
        System.out.println("Current season is " + myPlanet.getCountryWeather());
        System.out.println("Number of times the planet rotates around the sun is " + 365);
        System.out.println("Number of characters in planet name = " + myPlanet.getName().length());

        switch (myPlanet.getCountryWeather()) {
            case "Spring" -> System.out.println("The weather is warm in the UK");
            case "Summer" -> System.out.println("The weather is hot in the UK");
            case "Autumn" -> System.out.println("The weather is cool in the UK");
            default -> System.out.println("The weather is cold in the UK");
        }
    }
}

2020.3中,此过程已简化。您须要选择要提取的完整代码块,而后能够在macOS上使用⌘⌥M,在Windows和Linux上使用Ctrl + Alt + M来提取方法。code

提取方法选择

咱们能够给新方法起一个名字,例如,getWeather()而且IntelliJ IDEA将用对咱们新方法的调用替换原始逻辑:视频

public class PlanetExtractions {
    public static final int NUMBER_OF_DAYS_IN_A_YEAR = 365;
    Planet myPlanet = new Planet("earth");
    private String theWeatherIs = "The weather is";
    // I'm using PlanetExtractions to get the facts for my country
    // I'm using planetextractions to get the facts for my country
    private void printPlanetFacts(final String country) {
        System.out.println("Planet name is " + myPlanet.getName());
        System.out.println("Current season is " + myPlanet.getCountryWeather());
        System.out.println("Number of times the planet rotates around the sun is " + NUMBER_OF_DAYS_IN_A_YEAR);
        System.out.println("Number of characters in planet name = " + myPlanet.getName().length());
        getWeather();
    }
    private void getWeather() {
        switch (myPlanet.getCountryWeather()) {
            case "Spring" -> System.out.println("The weather is warm in the UK");
            case "Summer" -> System.out.println("The weather is hot in the UK");
            case "Autumn" -> System.out.println("The weather is cool in the UK");
            default -> System.out.println("The weather is cold in the UK");
        }
    }
}

经过再次使用相同的快捷方式,您能够获取“提取方法”的其余选项。

提取方法选项

提示:您能够经过在macOS上使用⌘B或在Windows和Linux上使用Ctrl + B导航到方法的声明或用法。

查看视频中的步骤

提取常数

咱们能够在此代码行中将数字365提取为常数,由于地球始终须要大约365天才能完成绕太阳的自转:

System.out.println("Number of times the planet rotates around the sun is " + 365);

咱们能够选择数字,而后在macOS上使用⌘⌥C,在Windows和Linux上使用Ctrl + Alt + C将其提取为常数。咱们能够给它起一个名字,例如 NUMBER_OF_DAYS_IN_A_YEAR。IntelliJ IDEA在课程开始时建立一个新的公共静态最终常量:

public static final int NUMBER_OF_DAYS_IN_A_YEAR = 365;

IntelliJ IDEA还用新的常量替换了原始代码行:

System.out.println("Number of times the planet rotates around the sun is " + NUMBER_OF_DAYS_IN_A_YEAR);

查看视频中的步骤

提取字段

在咱们提取的方法中,短语“天气是”重复了四次,所以让咱们将其提取到字段中:

 

private void getWeather() {
        switch (myPlanet.getCountryWeather()) {
            case "Spring" -> System.out.println("The weather is warm in the UK");
            case "Summer" -> System.out.println("The weather is hot in the UK");
            case "Autumn" -> System.out.println("The weather is cool in the UK");
            default -> System.out.println("The weather is cold in the UK");
        }
    }

 

您须要选择The weather is,而后能够在macOS上使用⌘⌥F,或在Windows和Linux上使用Ctrl + Alt + F将其提取到字段中。在“介绍字段”对话框中,咱们能够选择在“字段声明”中初始化该字段,为其指定一个名称,例如,theWeatherIs而后选择替换代码中全部四个出现的字段。

介绍字段对话框

当咱们按OK时,IntelliJ IDEA在班级顶部建立一个新字段:

String theWeatherIs = "The weather is";

IntelliJ IDEA还使用新字段更新了该方法:

 

private void getWeather() {
        switch (myPlanet.getCountryWeather()) {
            case "Spring" -> System.out.println(theWeatherIs + " warm in the UK");
            case "Summer" -> System.out.println(theWeatherIs + " hot in the UK");
            case "Autumn" -> System.out.println(theWeatherIs + " cool in the UK");
            default -> System.out.println(theWeatherIs + " cold in the UK");
        }
    }

查看视频中的步骤

提取变量

这样的连接方法可能很难理解,所以让咱们将其重构为一个单独的变量:

int planetNameLength = myPlanet.getName().length();

您能够将光标放在连接的方法调用中,而后使用箭头键选择要提取到新变量的数量。

提取变量下拉列表

让咱们提取myPlanet.getName().length()部分。咱们能够在macOS上使用⌘⌥V,在Windows和Linux上使用Ctrl + Alt + V将其提取为变量并命名 planetNameLength。如今咱们有了一个用于此计算的局部变量:

System.out.println("Planet name is " + myPlanet.getName().length());

IntelliJ IDEA还建立了咱们的新变量:

int planetNameLength = myPlanet.getName().length();

…并用新变量替换了咱们的代码:

System.out.println("Number of characters in planet name = " + planetNameLength);

查看视频中的步骤

提取参数

让咱们UK在此代码中提取国家/地区,并将其做为参数传递给country:

 

private void getWeather() {
        switch (myPlanet.getCountryWeather()) {
            case "Spring" -> System.out.println(theWeatherIs + " warm in the UK");
            case "Summer" -> System.out.println(theWeatherIs + " hot in the UK");
            case "Autumn" -> System.out.println(theWeatherIs + " cool in the UK");
            default -> System.out.println(theWeatherIs + " cold in the UK");
        }
    }

 

为此,让咱们选择UK并在macOS上使用⌘⌥P,在Windows和Linux上使用Ctrl + Alt + P。咱们能够给它起一个名字,例如country。我将要求IntelliJ IDEA替换全部四个实例,而后选择“重构”。IntelliJ IDEA更新了咱们的方法签名参数:

getWeather("UK");

…并替换其在文本中的全部四个出现:

private void getWeather(String country) {
        switch (myPlanet.getCountryWeather()) {
            case "Spring" -> System.out.println(theWeatherIs + " warm in the " + country);
            case "Summer" -> System.out.println(theWeatherIs + " hot in the " + country);
            case "Autumn" -> System.out.println(theWeatherIs + " cool in the " + country);
            default -> System.out.println(theWeatherIs + " cold in the " + country);
        }
    }

查看视频中的步骤

摘录摘要

提取重构的快捷方式具备对称性,能够帮助您记住它们。

对于macOS,它们是⌘⌥ +您要提取的内容的首字母,对于Windows和Linux,它们是Ctrl + Alt +您要提取的内容的首字母:

  • 中号编制方法
  • ç onstants
  • ˚F ields
  • V ariables
  • P arameters

是的,我知道这在技术上是五合一的,可是我真的想向您展现快捷方式的对称性,由于它有助于我更快地学习。在继续进行第二种主要重构类型以前,若是您改变主意,而且要内联先前提取的内容,该怎么办?在下文中我将详细讲解这个问题!

相关文章
相关标签/搜索