Java静态导入

Java静态导入

静态导入的语法是:

  import static ...;

静态导入的好处就是可以简化一些操作,例如System.out.println(…);就可以将其写入一个静态方法  import static java.lang.System.out;   ,在在下面使用时直接print(…)就可以了。

先带来一个最初的方法:

 1 public class Demo {
 2 
 3     public static void main(String[] args) {
 4         
 5         System.out.println(Math.max(3,4));
 6         System.out.println(Math.min(3,4));
 7         System.out.println(Math.pow(4,2));
 8         
 9     }
10 
11 }

使用了静态导入之后:

 1 import static java.lang.Math.*;
 2 public class Demo {
 3 
 4     public static void main(String[] args) {
 5         
 6         System.out.println(max(3,4));
 7         System.out.println(min(3,4));
 8         System.out.println(pow(4,2));
 9         
10     }
11 
12 }

可见静态导入方便了写代码,但在平常做题中不会写太多,在只有几行的情况下还是使用原来的方法快,而且方便查看

原文地址:https://www.cnblogs.com/shenhx666/p/8000923.html