ITeye博客

static import

yangyi · 2011-05-22 10:51 · Java · 编程语言 · Markdown

Static import支持略去类型名的public static字段使用

如: import static java.lang.Math.PI; import static java.lang.Math.pow; 或全部: import static java.lang.Math.*;

例子:

public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("Hello World!");
        System.out.println("Considering a circle with a diameter of 5 cm, it has:");
        System.out.println("A circumference of " + (Math.PI * 5) + "cm");
        System.out.println("And an area of " + (Math.PI * Math.pow(5,2)) + "sq. cm");
    }
}

简化为:

import static java.lang.Math.*;
import static java.lang.System.out;
public class HelloWorld {
    public static void main(String[] args) {
        out.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(5,2)) + "sq. cm");
    }
}

一些简单的语法糖,好像没什么大用

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。