Java中的导入语句
Java中的Import语句有助于在单个语句的帮助下为包下指定的程序获取一个类或所有类可见。这是非常有益的,因为程序员不需要编写整个类定义。因此,它提高了程序的可读性。
本文重点介绍Java程序中使用的 import 语句及其替代方法。
语法 1:
import package1[.package2].(*);
这里,
- package1:顶级包
- package2: package1下的下级包
- *:导入所有类
语法 2:
import package1[.package2].(myClass);
这里,
- package1:顶级包
- package2:顶级包下的下级包
- myClass:仅导入 myClass
Note: Either we can import a class or we can import all classes specified under the package.
为了理解为什么我们需要让一个或多个类可见,让我们考虑一个不使用 import 语句的Java程序:
源代码:
Java
// Java program to demonstrate the
// working of a program
// without any import statement
class GFG {
// Main method
public static void main(String[] args)
{
// Declaring an ArrayList of String type
ArrayList arrayList
= new ArrayList();
// Adding elements in the ArrayList
arrayList.add("Geeks");
arrayList.add("For");
arrayList.add("Geeks");
// Print the ArrayList
System.out.println("ArrayList: " + arrayList);
}
}
Java
// Java program to demonstrate the working of a program
// using fully-qualified name or without the use of import
// statement
class GFG {
// Main method
public static void main(String[] args)
{
// Using fully-qualified name
// Declaring an ArrayList of String type
java.util.ArrayList arrayList
= new java.util.ArrayList();
// Adding elements in the ArrayList
arrayList.add("Geeks");
arrayList.add("For");
arrayList.add("Geeks");
// Print the ArrayList
System.out.println("ArrayList: " + arrayList);
}
}
Java
// Java program to demonstrate the
// working of import statement
// Importing ArrayList class specified
// under java.util package
import java.util.ArrayList;
class GFG {
// Main method
public static void main(String[] args)
{
// Declaring an ArrayList of String type
ArrayList arrayList
= new ArrayList();
// Adding elements in the ArrayList
arrayList.add("Geeks");
arrayList.add("For");
arrayList.add("Geeks");
// Print the ArrayList
System.out.println("ArrayList: " + arrayList);
}
}
Java
// Java program to demonstrate the
// working of import statement
// Importing all classes specified under java.util package
import java.util.*;
class GFG {
// Static function to print array elements
public static void print(int array[])
{
System.out.print("Array: [ ");
for (int i = 0; i < 5; i++)
System.out.print(array[i] + " ");
System.out.print("]");
}
// main method
public static void main(String[] args)
{
// Declaring an ArrayList of String type
ArrayList arrayList
= new ArrayList();
// Adding elements in the ArrayList
arrayList.add("Geeks");
arrayList.add("For");
arrayList.add("Geeks");
// Declaring an array of integers
int array[] = { 10, 3, 5, 11, 20 };
// sort function defined under Arrays class
Arrays.sort(array);
// Print the ArrayList
System.out.println("ArrayList: " + arrayList);
// Calling print() function
print(array);
}
}
Java
// Statement to create a package
package CustomPackage;
// Public class
public class ComputerScienceDepartment {
String name;
int marks;
// user defined constructor method
public ComputerScienceDepartment(String studentName,
int studentMarks)
{
// Assigning values to data
// members with the help of
// values passed to the constructor
name = studentName;
marks = studentMarks;
}
// Display method to display student's information
public void display()
{
System.out.println("Name: " + name + '\n'
+ "Marks : " + marks + '\n'
+ "Department: "
+ "Computer Science" + '\n');
}
}
Java
// Import the public class defined under
// the created package (CustomPackage)
import CustomPackage.ComputerScienceDepartment;
class GeeksforGeeks {
// main method
public static void main(String args[])
{
// Initializing a variable by passing name
// and marks of the student as an argument to
// the constructor
ComputerScienceDepartment student1
= new ComputerScienceDepartment("Geeks", 97);
// Using display() method defined under
// ComputerScienceDepartment class to print sudent1
// information
student1
.display(); // you may also call the display()
// Initializing another variable by passing name
// and marks of the student as an argument to
// the constructor
ComputerScienceDepartment student2
= new ComputerScienceDepartment("GeeksforGeeks",
100);
// Using display() method defined under
// ComputerScienceDepartment class to print sudent2
// information
student2
.display(); // you may also call the display()
}
}
Java
// Java program to illustrate the working
// of static import statement
class GFG {
// Main method
public static void main(String[] args)
{
// Initializing two variables of integer type
int number1 = 10;
int number2 = 20;
// Using fully-qualified name to access
// abs() method - Math.abs()
System.out.println(
"Absolute difference of number1 and number2 is: "
+ Math.abs(number1 - number2));
}
}
Java
// Java program to illustrate the working
// of static import statement
// Import static statement
import static java.lang.Math.*;
class GFG {
// Main method
public static void main(String[] args)
{
// Initializing two variables of integer type
int number1 = 10;
int number2 = 20;
// Directly accessing abs() method without using
// fully-qualified name
System.out.println(
"Absolute difference of number1 and number2 is: "
+ abs(number1 - number2));
}
}
让我们编译上面的程序:
编译器判断:
prog.java:11: error: cannot find symbol
ArrayList arrayList
^
symbol: class ArrayList
location: class GFG
prog.java:12: error: cannot find symbol
= new ArrayList();
^
symbol: class ArrayList
location: class GFG
2 errors
编译描述:我们得到编译时错误。 Javac 编译器找不到 ArrayList 程序中的类。 ArrayList 类是Java.util 包的一部分。所以我们需要在我们的程序中包含Java.util 包下定义的ArrayList 类。
以下是引用外部类或包下指定的所有外部类的三种方法:
一、全称:
import 语句是可选的,我们可以使用类的完全限定名来引用程序中的类或包。此方法告诉编译器该类是在特定包下定义的,我们希望在程序中使用该类或多个类。每次我们想要使用类中定义的数据成员或成员函数时,都需要使用完全限定名称来引用它。它增加了我们程序的代码大小,因此使其可读性降低。这是这种方法的唯一缺点。
示例:下面是使用Java.util 包下定义的类 ArrayList 的完全限定名称的实现:
Java
// Java program to demonstrate the working of a program
// using fully-qualified name or without the use of import
// statement
class GFG {
// Main method
public static void main(String[] args)
{
// Using fully-qualified name
// Declaring an ArrayList of String type
java.util.ArrayList arrayList
= new java.util.ArrayList();
// Adding elements in the ArrayList
arrayList.add("Geeks");
arrayList.add("For");
arrayList.add("Geeks");
// Print the ArrayList
System.out.println("ArrayList: " + arrayList);
}
}
ArrayList: [Geeks, For, Geeks]
2.进口声明:
import 语句告诉编译器一个类或整个包的路径。它不同于 C++ 中的“#include”,它包含程序中的全部代码。 Import 语句告诉编译器我们要使用在包下定义的一个(或多个)类。它非常有用,推荐使用“完全限定名称”方法 因为它减少了整体代码大小并提高了源代码的可读性。
下面是说明如何将类导入程序的实现:
Java
// Java program to demonstrate the
// working of import statement
// Importing ArrayList class specified
// under java.util package
import java.util.ArrayList;
class GFG {
// Main method
public static void main(String[] args)
{
// Declaring an ArrayList of String type
ArrayList arrayList
= new ArrayList();
// Adding elements in the ArrayList
arrayList.add("Geeks");
arrayList.add("For");
arrayList.add("Geeks");
// Print the ArrayList
System.out.println("ArrayList: " + arrayList);
}
}
ArrayList: [Geeks, For, Geeks]
下面是说明如何将所有类导入程序的实现:
源代码:
Java
// Java program to demonstrate the
// working of import statement
// Importing all classes specified under java.util package
import java.util.*;
class GFG {
// Static function to print array elements
public static void print(int array[])
{
System.out.print("Array: [ ");
for (int i = 0; i < 5; i++)
System.out.print(array[i] + " ");
System.out.print("]");
}
// main method
public static void main(String[] args)
{
// Declaring an ArrayList of String type
ArrayList arrayList
= new ArrayList();
// Adding elements in the ArrayList
arrayList.add("Geeks");
arrayList.add("For");
arrayList.add("Geeks");
// Declaring an array of integers
int array[] = { 10, 3, 5, 11, 20 };
// sort function defined under Arrays class
Arrays.sort(array);
// Print the ArrayList
System.out.println("ArrayList: " + arrayList);
// Calling print() function
print(array);
}
}
ArrayList: [Geeks, For, Geeks]
Array: [ 3 5 10 11 20 ]
现在让我们通过在其中创建自定义包和类来查看导入语句的工作。我们可以在所有类定义之前包含以下语句,以将程序捆绑到一个包中。
句法:
package myPackage;
这里,
- myPackage:包的名称
下面是说明我们如何通过创建自定义包来导入类的实现:
Java
// Statement to create a package
package CustomPackage;
// Public class
public class ComputerScienceDepartment {
String name;
int marks;
// user defined constructor method
public ComputerScienceDepartment(String studentName,
int studentMarks)
{
// Assigning values to data
// members with the help of
// values passed to the constructor
name = studentName;
marks = studentMarks;
}
// Display method to display student's information
public void display()
{
System.out.println("Name: " + name + '\n'
+ "Marks : " + marks + '\n'
+ "Department: "
+ "Computer Science" + '\n');
}
}
我们在系统本地创建了上述Java源文件,并将其保存为ComputerScienceDepartment。Java:
现在让我们使用 javac 编译器编译程序并通过命令提示符将其捆绑为一个包,
javac ComputerScienceDepartment.java
javac -d . ComputerScienceDepartment.java
编译说明:如上图所示,生成CustomPackage文件夹,里面包含ComputerScienceDepartment.class字节码文件。该文件稍后由Java解释器 (JVM) 执行。现在包已经准备好了,我们可以创建另一个程序并将这个包导入到程序中。
以下是包含/导入程序中 CustomPackage 下定义的 ComputerScienceDepartment 类的源代码:
Java
// Import the public class defined under
// the created package (CustomPackage)
import CustomPackage.ComputerScienceDepartment;
class GeeksforGeeks {
// main method
public static void main(String args[])
{
// Initializing a variable by passing name
// and marks of the student as an argument to
// the constructor
ComputerScienceDepartment student1
= new ComputerScienceDepartment("Geeks", 97);
// Using display() method defined under
// ComputerScienceDepartment class to print sudent1
// information
student1
.display(); // you may also call the display()
// Initializing another variable by passing name
// and marks of the student as an argument to
// the constructor
ComputerScienceDepartment student2
= new ComputerScienceDepartment("GeeksforGeeks",
100);
// Using display() method defined under
// ComputerScienceDepartment class to print sudent2
// information
student2
.display(); // you may also call the display()
}
}
我们已经在系统本地创建了上述Java源文件,并将其保存为GeeksforGeeks。Java:
现在让我们使用 javac 编译器编译程序并通过命令提示符运行它,
javac GeeksforGeeks.java
java GeeksforGeeks
输出:
3.静态导入语句:
通过在Java中使用静态导入语句,我们可以直接访问类的数据成员或成员函数,而无需使用完全限定名称。
下面是我们访问 Math 类下的 abs() 方法的实现,而不使用任何静态导入语句并使用完全限定名称:
Java
// Java program to illustrate the working
// of static import statement
class GFG {
// Main method
public static void main(String[] args)
{
// Initializing two variables of integer type
int number1 = 10;
int number2 = 20;
// Using fully-qualified name to access
// abs() method - Math.abs()
System.out.println(
"Absolute difference of number1 and number2 is: "
+ Math.abs(number1 - number2));
}
}
Absolute difference of number1 and number2 is: 10
下面是使用静态导入语句的实现。
Java
// Java program to illustrate the working
// of static import statement
// Import static statement
import static java.lang.Math.*;
class GFG {
// Main method
public static void main(String[] args)
{
// Initializing two variables of integer type
int number1 = 10;
int number2 = 20;
// Directly accessing abs() method without using
// fully-qualified name
System.out.println(
"Absolute difference of number1 and number2 is: "
+ abs(number1 - number2));
}
}
Absolute difference of number1 and number2 is: 10
好处:
- 导入语句有助于减少代码大小,从而节省大量时间。
- 它提高了我们代码的可读性。
- 在处理大型项目时非常有用。
- 它们可用于将多个类的功能组合为一个。