JDK5新特性之一----静态导入
现在让我们来看看JDK5中有什么更激动人心的改进和创新,我要介绍的是其中的一个比较实用的特性:静态导入!
直接看一段代码吧,很容易就可以了解:
package staticEx;
public class TranscendentalConstants { public static final double PI = 3.14159; public static final double E = 2.71828; } public class IrrationalConstants { public static final double SQRT_TWO = 1.414; public static final double SQRT_THREE = 1.732; } //在类中导入静态变量和方法 package staticEx;
import static staticEx.IrrationalConstants.SQRT_TWO; import static staticEx.IrrationalConstants.SQRT_THREE; import static staticEx.TranscendentalConstants.PI;
public class ConstantsWithStaticImport {
public static double sinPiOverFour() { return SQRT_TWO / 2; }
public static void main(String[] args) { System.out.println("Pi is approximately " + PI); System.out.println("The sin of Pi/4 is about " + sinPiOverFour()); } }
|
JDK5提供了导入静态类,方法,和属性的新特性,上手也很容易,至于适用的地方嘛,各位就自己看着办罗!