当我导入 System
类时,我不明白关键词 stistic
的含义:
import static java.lang.System.*
我在读关于爪哇的书 书写在那里:
Any import declaration that doesn t use the word
static
must start with the name of a package and must end with either of the following:
- The name of a class within that package
- An asterisk (indicating all classes within that package)
For example, the declaration import
java.util.Scanner;
is valid becausejava.util
is the name of a package in the Java API, andScanner
is the name of a class in thejava.util
package.Here’s another example. The declaration
import javax.swing.*;
is valid becausejavax.swing
is the name of a package in the Java API, and the asterisk refers to all classes in thejavax.swing
package.
我有以下代码:
public class Addition {
public static void main(String[] args) {
double num;
num = 100.53;
num = num + 1000;
// So when I want to import the whole package java.lang as written in the book, it doesn t work:
// import java.lang.*;
// or like this:
// import static java.lang.*;
// NetBeans in both cases doesn t see these abbreviated names `out` and throws errors. Why?
out.print("The result is ");
out.print(num);
out.println(" .");
}
}
当我这样进口的时候,它就起作用了:
import static java.lang.System.out;
import static java.lang.System.*
但当我尝试这样做时却不管用:
import java.lang.System.out;
import java.lang.System.*
在此特定情况下, stistic
关键字的含义是什么?
为什么 import java.lang.*;
不导入整个包件, 包括 System
类?