静态导入: 语句定义格式:import static 包名...类名.方法名; 可以直接导入到方法级别 注意事项: 方法必须是静态的import static java.lang.Math.abs; import static java.lang.Math.pow; import static com.shujia.zgq.day19.StaticClass.fun; import static com.shujia.zgq.day19.StaticClass.show; import static java.lang.Math.max; public class StaticImportDemo { public static void main(String[] args) { //Math System.out.println(Math.abs(-100));//绝对值 System.out.println(Math.pow(2, 3)); System.out.println(Math.max(100, 200)); //有没有什么方法,不同写类名,直接写方法名? // System.out.println(abs(-200)); //这时候,就需要静态导入的技术 System.out.println(abs(-200)); System.out.println(pow(2, 4)); fun(); //当静态导入的方法名与本类中的方法名冲突的时候,调用的是本类中的方法 show("spark");//调用本类的show方法 //如果此时我就是想使用静态导入的方法,怎么办? // StaticClass.show("flink"); //
Java--静态导入
最新推荐文章于 2024-05-10 20:08:31 发布