定义一个包含常量的接口。即我们熟知的反模式 常量接口
Constants.java
package maoxiang.examples.jdk15.staticimport;
/**
* @author 毛翔
*
* 常量接口
*/
public interface Constants {
public static float PI = 3.1415926f;
}
使用常量接口 Demo.java
package maoxiang.examples.jdk15.staticimport;
import static maoxiang.examples.jdk15.staticimport.Constants.PI;
/**
* @author 毛翔
*
*
*/
public class Demo {
public static void main(String[] args) {
Test1();
}
public static void Test1(){
System.out.print(PI);
}
}
博客介绍了JDK中常量接口的定义与使用,给出了定义常量接口的代码示例,定义了包含常量PI的接口Constants,还展示了使用该常量接口的代码,通过静态导入在Demo类中使用常量PI。
514

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



