我使用NetBeans IDE在JavaFX中執行項目。 IDE會自動導入所有必需的導入語句。它具體做到這一點,那只是必要的進口報表被導入。以下是我的一個Java應用程序中的導入語句列表 -爲什麼NetBeans IDE專門使用import語句?
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 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;
這節省了大量的代碼行。
兩組進口報表之間的實際差異是什麼?爲什麼NetBeans特別使用import語句?
從我已閱讀和理解的內容中,導入這樣一個原因是它可以改進編譯。它還可以減少兩個類具有相同名稱時的歧義性 – MadProgrammer
@MadProgrammer您的意思是「顯式」:)它也使得所有類的依賴性變得明顯,並且不會由於編譯類路徑中的更改而隱含地更改代碼語義。 –
如果您使用高級IDE之一,您可以將導入摺疊爲1行:-) –