ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2020-12-18 10:59:28  阅读:376  来源: 互联网

标签:case IDEA IntelliJ System 2020.3 UK println 提取 out


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

下载IntelliJ IDEA 2020.3

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

简化代码具有很多优势,包括提高可读性,解决技术难题以及管理不断变化的需求。我们将在此博客中介绍三种重构类型:

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

提取和内联

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

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

提取方法

此方法中的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来提取方法。

提取方法选择

我们可以给新方法起一个名字,例如,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

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

标签:case,IDEA,IntelliJ,System,2020.3,UK,println,提取,out
来源: https://blog.csdn.net/qq_42444778/article/details/111354903

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有