【发布时间】:2013-07-11 21:53:32
【问题描述】:
下面的类在println("Hello World!"); 行导致编译错误:The method println(String) is undefined for the type StaticImport:
import static java.lang.Math.*;
import static java.lang.System.*;
public class StaticImport {
public static void main(String[] args) {
println("Hello World!");
out.println("Considering a circle with a diameter of 5 cm, it has:");
out.println("A circumference of " + (PI * 5) + " cm");
out.println("And an area of " + (PI * pow(2.5,2)) + " sq. cm");
}
}
为什么不显式导入 pow 就可以在 java.lang.Math 中访问 pow 方法,不像 println 方法需要使用方法名“out”?
【问题讨论】:
-
out是System的静态成员,包含一个 PrintWriter 引用。println不是静态方法,而是 PrintWriter 实例的实例方法。
标签: java static-import