1. Overview
1.概述
In this tutorial, we’ll discuss the advantages and disadvantages of using wildcard imports in Java.
在本教程中,我们将讨论在Java中使用通配符导入的优点和缺点。
2. Imports in Java
2.Java中的进口
Java import statements declare the source of the names (class names, static variables, and method names) used in the code.
Java import语句声明了代码中使用的名称(类名、静态变量和方法名)的来源。
As an example, let’s look at a Book class:
作为一个例子,让我们看看一个Book类:
import java.util.Date;
import java.util.List;
import java.util.UUID;
public class Book {
private UUID id;
private String name;
private Date datePublished;
private List<String> authors;
}
Here, we need to import the two data types of Date and UUID along with the List interface as they are not available by default. So, we write three import statements to have these data types available for our class. Let’s refer to these types of imports as specific imports.
在这里,我们需要导入Date和UUID这两种数据类型以及List接口,因为它们默认是不可用的。因此,我们写了三条导入语句,以使这些数据类型对我们的类可用。让我们把这些类型的导入称为特定的导入。。
3. Java Wildcard Imports
3.Java通配符进口
Wildcard imports refer to importing a package instead of declaring specific class names being used from a package.
通配符导入是指导入一个包,而不是声明从包中使用的特定类名。。
Using wildcards, we can replace the three import statements from our previous example with just one statement:
使用通配符,我们可以用一条语句来替换前面例子中的三个导入语句。
import java.util.*;
public class Book {
private UUID id;
private String name;
private Date datePublished;
private List<String> authors;
}
This one wildcard import statement adds the entire java.util package to the search path, where the required names of UUID, Date, and List can be found.
这一个通配符import语句将整个java.util包添加到搜索路径中,在那里可以找到UID、Date和List的必要名称。
4. Advantages of Wildcard Imports
4.通配符进口的优势
Naturally, wildcard imports have some advantages compared to specific imports in Java. Let’s discuss the main advantages of wildcard imports in the subsections below.
当然,与Java中的特定导入相比,通配符导入有一些优势。让我们在下面的小节中讨论通配符导入的主要优势。
4.1. Clean Code
4.1.清洁代码
Wildcard imports help us avoid a long list of imports in our code. Therefore, this impacts the readability of code as the reader may have to scroll a lot in every source code file before reaching the code that shows the logic. Undoubtedly, more readable code is also clean code.
通配符导入帮助我们避免在代码中出现长长的导入列表。因此,这影响了代码的可读性,因为读者在到达显示逻辑的代码之前,可能不得不在每个源代码文件中滚动很多。毫无疑问,更可读的代码也是干净的代码。
This idea is also supported in the Clean Code book by Robert C. Martin. In fact, the book recommends using wildcard imports when using multiple classes from the same source. In other words, when we import two or more classes imported from a package, it’s better to import the whole package.
这个想法在Robert C. Martin的Clean Code书中也得到支持。事实上,该书推荐在使用同一源的多个类时使用通配符导入。换句话说,当我们导入从一个包中导入的两个或多个类时,最好是导入整个包。
4.2. Refactoring Ease
4.2.重构的便利性
With wildcard imports, refactoring is easier. For instance, upon renaming a class, we don’t need to remove all of its specific import declarations.
有了通配符导入,重构就更容易了。例如,在重命名一个类时,我们不需要删除其所有特定的导入声明。
Also, if we moved a class from one of our packages to another of our own packages, we don’t need to refactor any code if wildcard imports already exist in the file for both of the packages.
另外,如果我们将一个类从我们的一个包中移到我们自己的另一个包中,如果通配符导入已经存在于两个包的文件中,我们不需要重构任何代码。
4.3. Loose Coupling
4.3.松动的联接
Wildcard imports enforce the loose-coupling approach in modern software development.
通配符导入执行了现代软件开发中的松散耦合方法。
According to Robert C. Martin, the idea of having wildcard imports enforces loose-coupling. With specific imports, the class must exist in a package. However, with wildcard imports, particular classes don’t need to exist in the package. In fact, the wildcard import adds the specified package to the search path, where required class names can be searched.
根据Robert C. Martin的说法,拥有通配符导入的想法是为了执行松耦合。对于特定的导入,该类必须存在于一个包中。然而,通过通配符导入,特定的类不需要存在于包中。事实上,通配符导入将指定的包添加到搜索路径中,在那里可以搜索到所需的类名。
Hence, wildcard-styled imports add no true dependency to the package.
因此,通配符风格的导入没有给包增加真正的依赖性。
5. Disadvantages of Wildcard Imports
5.通配符进口的劣势
Wildcard imports have their disadvantages too. Next, let’s look at how wildcard imports can lead to some problems.
通配符导入也有其缺点。接下来,让我们看一下通配符导入如何导致一些问题。
5.1. Class Name Conflicts
5.1.类名冲突
Unfortunately, conflicts can occur when a class name is found in more than one package imported through wildcard.
不幸的是,当一个类的名字在通过通配符导入的一个以上的包中被发现时,就会发生冲突。
In this case, the compiler notices that there are two Date classes and gives an error since the Date class is found in both the java.sql and java.util packages:
在这种情况下,编译器注意到有两个Date类,并给出了一个错误,因为Date类在java.sql和java.util包中都有。
import java.util.*;
import java.sql.*;
public class Library {
private UUID id;
private String name;
private Time openingTime;
private Time closingTime;
private List<Date> datesClosed;
}
To prevent such an error, we can specify the desired source of the conflicted class.
为了防止出现这样的错误,我们可以指定冲突类的理想来源。
To prevent the error in the example above, we can add a third line specifying the source of the conflicted Date class to the two existing imports:
为了防止上面的例子中出现错误,我们可以在现有的两个导入中添加第三行,指定冲突的Date类的来源。
import java.util.*;
import java.sql.*;
import java.sql.Date;
5.2. Unforeseen Class Name Conflicts
5.2.不可预见的类别名称冲突
Interestingly, conflicts can also surface over time, such as when a class is added to the newer version of another package that we are using.
有趣的是,冲突也会随着时间的推移而浮现,例如当一个类被添加到我们正在使用的另一个包的较新版本中时。
For instance, in Java 1.1, the List class was only found in the java.awt package. However, with Java 1.2, an interface named List was added to the java.util package.
例如,在Java 1.1中,List类只能在java.awt包中找到。然而,在Java 1.2中,一个名为List的接口被添加到java.util包中。
Let’s see an example:
让我们看一个例子:
import java.awt.*;
import java.util.*;
public class BookView extends Frame {
private UUID id;
private String name;
private Date datePublished;
private List<String> authors;
}
Eventually, this situation can potentially cause a conflict when both java.awt and java.util packages are imported as wildcard imports. Hence, we can potentially face problems when migrating code to a newer Java version.
最终,当java.awt和java.util包都作为通配符导入时,这种情况有可能导致冲突。因此,当将代码迁移到较新的Java版本时,我们有可能面临问题。