今天在看源码的时候看到这个特性 下面记录下
静态导入
功能介绍:
import:单纯import关键字可以导入一个类或某个包中的所有类
import它虽然是可以导入类,但是你在使用类方法或类属性的时候还需要加上类名(字写的比较多难受),所以就有了Import static
import static:import static语句导入一个类中的某个静态成员(方法或属性)或所有静态成员
下面是例子:
package test.dlmu.importstatic;
/**
*
* @author hadoop
*
*/
public class ImportStatic {
public static String DLMU = "dlmu";
public static String getDlmu(){
return DLMU;
}
}
package test.dlmu.importstatic;
import static test.dlmu.importstatic.ImportStatic.DLMU;
import static test.dlmu.importstatic.ImportStatic.getDlmu;
/**
*
* @author hadoop
*
*/
public class ImportStaticTest {
/**
*
* @param args
*/
public static void main(String[] args){
System.out.println(ImportStatic.getDlmu());
System.out.println(ImportStatic.DLMU);
//如果引入了import static 就不需要写ImportStatic 就想使用本类的方法一样
System.out.println(getDlmu());
System.out.println(DLMU);
}
}