`
yangyi
  • 浏览: 112804 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

static import

    博客分类:
  • Java
阅读更多
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");
    }
}


一些简单的语法糖,好像没什么大用
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics