【问题标题】:Why does this static import not compile为什么此静态导入无法编译
【发布时间】: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”?

【问题讨论】:

  • outSystem 的静态成员,包含一个 PrintWriter 引用。 println 不是静态方法,而是 PrintWriter 实例的实例方法。

标签: java static-import


【解决方案1】:

使用静态导入,您可以访问类的直接成员而无需完全限定它。您的静态导入允许您直接访问out,因为它是System 的成员,并且直接访问pow,因为它是Math 的成员。但是MathSystem 都没有println 的方法; PrintWriter 确实(out 的类型)。

您的静态导入...

import static java.lang.System.*;
//...
println("Hello World!");

... 等价于下面的代码,我们可以看到它不会编译:

System.println("Hello World!");

【讨论】:

    【解决方案2】:

    println 是属于System 类的out 成员的方法。没有那个限定符,Java 不知道 println 是什么。

    当您执行静态导入时,您可以访问类的成员而无需提供完全限定名称。因此,您对java.lang.System 的静态导入可以让您访问System 拥有的所有内容。这意味着您可以访问属于SystemoutPrintWriter 实例)。但它确实意味着您可以直接访问println,因为它属于out。所以你必须先通过out 告诉Java 如何到达println

    这样看。假设你有以下结构:

    MyClass
       |
       +---Something
       |      |
       |      +---- methodOne
       |      |
       |      +---- methodTwo
       |      |
       |      +---- OtherThing
       |                |
       |                +---- otherMethodOne
       |                |
       |                +---- otherMethodTwo
       +---method
    

    如果您使用MyClass.* 进行了静态导入:

    • 您可以访问method()(Java 将其翻译为MyClass.method()
    • 您可以访问Something。 (Java 将其翻译为 MyClass.Something
    • 拥有对Something 下其他内容的原始访问权限。例如,您不能简单地调用methodOne(),因为Java 会将其转换为MyClass.methodOne(),并且MyClass 下没有称为methodOne() 的方法。那个方法属于Something,所以你可以Something.methodOne(),或者Something.OtherThing.otherMethodOne()

    如果您使用 MyClass.Something.* 进行了静态导入:

    • 您可以访问methodOne。 (Java 将其翻译为 MyClass.Something.methodOne
    • 您可以访问methodTwo。 (Java 将其翻译为 MyClass.Something.methodTwo
    • 您可以访问OtherThing。 (Java 将其翻译为 MyClass.Something.OtherThing
    • 没有拥有otherMethodOneotherMethodTwo 的原始访问权限;你需要做OtherThing.otherMethodOne()OtherThing.otherMethodTwo()
    • 没有有权访问method,因为Java 将其转换为MyClass.Something.method,而Something 类中没有这样的方法。

    【讨论】:

      【解决方案3】:

      pow 是 Math 类的静态方法,因此如果您在顶部编写了 import java.lang.Math 语句,则无需创建 Math 类的对象即可访问它,另一方面 out 是 PrintStream 类型的 System 类的成员而 println() 是 PrintStream 类的一个方法,它不是静态的,所以你不能以静态的方式访问它,因为不创建对象就不能调用非静态函数。您还可以参考此链接了解有关 PrintStream 的更多信息 http://docs.oracle.com/javase/6/docs/api/java/io/PrintStream.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-15
        • 2012-10-12
        • 1970-01-01
        • 2015-11-18
        • 2011-04-28
        相关资源
        最近更新 更多