IntelliJ IDEA 2020.3提供了许多实用的功能,例如调试时的交互式提示,Git暂存支持,对Java 15记录和密封类的扩展支持等等。它简化了端点,框架和事件探查器的日常工作。通过基于机器学习技术的更好的代码完成,更直观和有用的新的“Welcome”屏幕以及更好的拼写和语法检查,整个UX得到了改进。简而言之,一切都更好!(本文主要讲述“提取”这种方法,点击查看完整版!)
这篇博客文章涵盖了与视频相同的内容,并包含一些其他提示和技巧。点击查看视频>>
简化代码具有很多优势,包括提高可读性,解决技术难题以及管理不断变化的需求。我们将在此博客中介绍三种重构类型:
- 提取和内联
- 更改签名
- 重命名
提取和内联
简化代码的第一种方法是提取它。您可以在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