J2SE 5.0新特性之static import
来源:互联网 发布:哪个软件调用rundll32 编辑:程序博客网 时间:2024/05/17 00:04
在《effective java》中作者曾经谈到在接口中定义常量是很糟糕的一种使用方法,我们应该始终使用接口来定义类型。但是在实际开发工作中还是有很多人这样使用接口,他们这样做的原因是这样定义常量使用起来很方便。例如如下定义方式:
public interface BadIrrationalConstants {
public static final double SQRT_TWO = 1.414;
public static final double SQRT_THREE = 1.732;
}
public interface BadTranscendentalConstants {
public static final double PI = 3.14159;
public static final double E = 2.71828;
}
如果你想在自己的类中使用这些接口中定义的常量的时候,那么你必须要实现这些接口。比如
public class BadUseOfConstants implements
BadTranscendentalConstants, BadIrrationalConstants {
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());
}
}
这样这些常量就变成你的类的一部分了,如果这个类不是final的话,其它的类继承了这个类又使得它继承了这些常量,但是这也许不是用户需要的结果。
针对这样的情况,我们可以这样做,那就是在一个类中定义这些常量,使用的时候可以通过ClassName.variableName来访问他们。例如
package staticEx;
public class IrrationalConstants {
public static final double SQRT_TWO = 1.414;
public static final double SQRT_THREE = 1.732;
}
package staticEx;
public class TranscendentalConstants {
public static final double PI = 3.14159;
public static final double E = 2.71828;
}
现在J2SE 5.0提供了静态导入的功能,你只需要在import关键字后面写一个static关键字就可以直接使用类中定义的常量了,例如
import static staticEx.IrrationalConstants.SQRT_TWO;
import static staticEx.IrrationalConstants.SQRT_THREE;
import static staticEx.TranscendentalConstants.PI;
当然你也可以使用.*的格式,例如
import static staticEx.IrrationalConstants.*;的格式
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());
}
}
运行该程序会得到
Pi is approximately 3.14159
The sin of Pi/4 is about 0.707
在这里提醒大家一下,如果你过多的使用.*的样式,那么可能会给程序的可读性带来负面的影响。因为你很难看出这个变量在哪里定义的。
对常量定义是如此,对静态方法的使用也适用。例如下面的例子
package staticEx;
public class IrrationalConstants2 {
public static final double SQRT_TWO = 1.414;
public static final double SQRT_THREE = 1.732;
public static double sinPiOverFour() {
return SQRT_TWO / 2;
}
}
package staticEx;
import static staticEx.IrrationalConstants2.*;
import static staticEx.TranscendentalConstants.*;
public class ConstantsWithStaticImport2 {
public static void main(String[] args) {
System.out.println("Pi is approximately " + PI);
System.out.println("The sin of Pi/4 is about " +
sinPiOverFour());
}
}
在ConstantsWithStaticImport2类中静态导入IrrationalConstants2后就可以直接使用它的静态方法sinPiOverFour()了。
以上简单的介绍了如何使用static import,您可以参考一下连接获得更多的介绍:
http://java.sun.com/developer/JDCTechTips/2004/tt1005.html
http://java.sun.com/j2se/1.5.0/docs/guide/language/static-import.html
- J2SE 5.0新特性之static import
- J2SE 5.0新特性之static import
- j2se中的新特性 import static
- J2SE 5.0新特性 之 线程
- J2SE 5.0新特性 之 线程
- [转]J2SE 5.0新特性 之 线程
- J2SE 5.0 新特性
- J2SE 5.0新特性
- J2SE 5.0新特性
- J2SE 5.0 语言新特性
- J2SE 5.0 的新特性
- j2se 5.0新特性之StringBuilder类应用
- J2SE 1.5新特性之For循环
- J2SE 5.0的新特性---Autoboxing
- J2SE 5.0的三个新特性
- J2SE 5.0新特性(一)枚举
- J2SE 5.0新特性(二)泛型
- J2SE 5.0新特性(一)枚举
- 刚做的一个网页计数器 大家参考一下
- 自己做的觉得好玩的第一个JAVA程序
- CSDN的气氛还是怪怪的
- 扩展ClassLoader定制类装载器
- 正确使用String类的几点注意
- J2SE 5.0新特性之static import
- 使用Proguard混淆J2ME应用程序
- WEB打印大全(http://www.pcsky.cn/article/list.asp?id=797)
- J2ME游戏开发中使用层的概念
- ANT学习笔记
- J2ME基础笔记
- 龟爬的速……
- 困惑
- 使用Flex结合Webservice完成域名查询