1.静态导入概述
格式:import static 包名...类名.方法名;
可以直接导入到方法的级别。
2.代码
package cn;
public class StaticImportDemo {
public static void main(String[] args) {
System.out.println(Math.abs(-1));//1
System.out.println(Math.max(2, 3));//3
}
}
package cn;
import static java.lang.Math.abs;
import static java.lang.Math.max;
public class StaticImportDemo {
public static void main(String[] args) {
System.out.println(abs(-1));//1
System.out.println(max(2, 3));//3
}
}
3.注意事项
3.1方法必须是静态的
3.2如果有多个同名的静态方法,容易不知道使用哪个?这个时候要使用,必须加前缀。由此有见,意义不大,所以一般不用,但是要了解哦。
转载于:https://blog.51cto.com/11841428/1862377