简体   繁体   English

Java需要进口澄清

[英]Java imports clarification needed

Since I'm still new to Java, I'm running into some need for clarification how imports should be done properly. 由于我还是Java新手,因此我需要澄清如何正确完成导入。

First: 第一:

import java.awt.*;
import java.swing.*;

I'm assuming the * means anything under the "awt" and "swing" directory. 我假设*表示“awt”和“swing”目录下的任何内容。 But I've seen people doing this before: 但我以前见过人们这样做过:

import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
import javax.swing.*;

Am I misunderstanding something? 我误会了什么吗? Or am I simply finding redundancy? 或者我只是找到冗余?

import javax.awt.*

Will include all of the classes in the package javax.awt however it will not inclue any other packages nested inside of javax.awt such as javax.awt.event . 将包含javax.awt包中的所有类,但是它不会包含嵌套在javax.awt任何其他包,例如javax.awt.event You need to use that as a separate import. 您需要将其用作单独的导入。 All packages of different names needed to be included separately. 所有不同名称的包都需要单独包含。

Using import javax.swing.* you will not need the following imports: 使用import javax.swing.*您将不需要以下导入:

import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

because they are direct children of the swing package. 因为他们是swing包的直接孩子。

In an import statement, the * indicates that you want everything from that package. 在import语句中,*表示您需要该包中的所有内容。 In general, this isn't a good idea - you don't need everything from the package you're including. 一般来说,这不是一个好主意 - 您不需要包含所有包装的所有内容。

In practice, I've seen imports optimized - merely import what you need. 在实践中,我已经看到导入优化 - 只是导入你需要的东西。 Most IDEs, such as Eclipse, Netbeans and IntelliJ have options to do just that for you. 大多数IDE,例如Eclipse,Netbeans和IntelliJ都可以选择为您做到这一点。

It's usually a good idea to use explicit imports and avoid using wildcards, so that you always know exactly what you are importing. 使用显式导入并避免使用通配符通常是个好主意,这样您就可以确切地知道要导入的内容。 However, the JVM will only import the required classes when you use wildcards (but not from sub packages), so there is no loss in efficiency (aside from a negligible compilation overhead). 但是,JVM只会在您使用通配符时导入所需的类(但不能从子包中导入),因此不会降低效率(除了可忽略的编译开销)。

It's not necessarily redundant to do this, but in my opinion a bit awkward: 这样做并不一定多余,但在我看来有点尴尬:

import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.*;

There might be another UnsupportedLookAndFeelException declared in another package so you want to be sure to include the one from javax.swing package, so it's made explicit in this case. 可能在另一个包中声明了另一个UnsupportedLookAndFeelException,因此您希望确保包含来自javax.swing包的那个,因此在这种情况下它是明确的。 Your best bet as mentioned is to allow the IDE to help you explicitly choose the import you want. 如上所述,您最好的选择是允许IDE帮助您明确选择所需的导入。

When you add * at the end of the import (wild card import)you are importing all classes included in certain package ie com.test.* . 当您在导入(通配符导入)结尾处添加*时,您将导入某些包中包含的所有类,即com.test.* On the other hand when you add for example a class you will specifically import that particular class ie com.test.TestClass 另一方面,当您添加一个类时,您将专门导入该特定类,即com.test.TestClass

However, when using the first approach you need to be aware of possible class collisions as explained in this stackoverflow thread: Eclipse/Java - is it harmful to import java.(namespace).*? 但是,在使用第一种方法时,您需要了解可能的类冲突,如此stackoverflow线程中所述: Eclipse / Java - 它对导入java。(命名空间)有害。*?

Due to that I prefer the second approach since I can explicitly tell which class I intend to use, and in that way it makes the code easier to read 由于我更喜欢​​第二种方法,因为我可以明确地告诉我打算使用哪个类,并以这种方式使代码更容易阅读

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM