Static import支持略去类型名的public static字段使用
如:
import static java.lang.Math.PI;
import static java.lang.Math.pow;
或全部:
import static java.lang.Math.*;
例子:
简化为:
一些简单的语法糖,好像没什么大用
如:
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");
}
}一些简单的语法糖,好像没什么大用
本文通过具体示例展示了如何在Java中使用静态导入(import static)来简化代码书写,特别是对于频繁使用的公共静态成员。
2234

被折叠的 条评论
为什么被折叠?



