我使用 NetBeans IDE 在 JavaFX 中执行一个项目。 IDE 自动导入所有必要的导入语句。它是专门这样做的,即仅导入必要的导入语句。以下是我的一个 Java 应用程序中的导入语句列表 -
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.stage.Stage;
正如所见,这是一长串导入语句。我发现的一个问题是它需要更多的代码行。他们为什么不使用如下所示的 import 语句?
import java.sql.*;
import java.util.logging.*;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.collections.*;
import javafx.event.*;
import javafx.geometry.Insets;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.*;
import javafx.scene.paint.Color;
import javafx.scene.text.*;
import javafx.stage.Stage;
这节省了大量代码。
两组 import 语句之间的实际区别是什么?为什么 NetBeans 专门使用 import 语句?
最佳答案
让我演示一下通配符导入最严重的问题之一:
import org.example.mylib.*;
import java.util.*;
class X {
List<String> xs;
}
在您的 mylib
的 2.0 版本中您添加一个名为 List<T>
的类。您根本不接触源代码,源代码与添加的类无关。
如果您添加了 List
定义类 X
使用的所有方法,代码将成功构建,但运行时行为将由于深奥的神秘原因而改变。
关于java - 为什么 NetBeans IDE 专门使用 import 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23039569/