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

本文介绍了IntelliJ IDEA 2020.3版本中重构代码的三种方法:提取方法、提取常数、提取字段。详细阐述了如何使用快捷键进行代码优化,提升代码可读性和维护性。
摘要由CSDN通过智能技术生成

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 -> Syste
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值