Java静态导入

例子1:

 1 package cn.itcast.day1;
 2 
 3 public class StaticImport {
 4     public static void main(String[] args)
 5     {
 6         int x = 1;
 7         System.out.println("Hello world!");
 8         System.out.println(Math.max(3, 4));
 9         System.out.println(Math.abs(3 - 23));
10     }
11 }

这里如果调用每个类的静态函数,则需要写上类名进行调用。

 例子2:

 1 package cn.itcast.day1;
 2 
 3 import static java.lang.Math.max;
 4 import static java.lang.Math.abs;
 5 
 6 public class StaticImport {
 7     public static void main(String[] args)
 8     {
 9         int x = 1;
10         System.out.println("Hello world!");
11         System.out.println(max(3, 4));
12         System.out.println(abs(3 - 23));
13     }
14 }

进行了静态导入之后,不需要写上类名即可使用其静态函数了。

例子3:

package cn.itcast.day1;

import static java.lang.Math.*;

public class StaticImport {
    public static void main(String[] args)
    {
        int x = 1;
        System.out.println("Hello world!");
        System.out.println(max(3, 4));
        System.out.println(abs(3 - 23));
    }
}

静态导入了类Math所有的静态函数。

原文地址:https://www.cnblogs.com/Robotke1/p/3100125.html