静态导入意味着如果将类中的字段和方法定义为公共静态,则无需指定其类即可在代码中使用它们。
sqrt()包java.lang中的Math类方法是静态导入的。
演示此过程的程序如下:
import static java.lang.Math.sqrt;
public class Demo {
public static void main(String[] arg) {
double num = 36.0;
System.out.println("The number is: " + num);
System.out.println("The square root of the above number is: " + sqrt(num));
}
}输出结果
The number is: 36.0 The square root of the above number is: 6.0
现在让我们了解上面的程序。
不需要Math类以及该方法,sqrt()因为java.lang包使用静态导入。显示数字num及其平方根。演示此代码段如下:
double num = 36.0;
System.out.println("The number is: " + num);
System.out.println("The square root of the above number is: " + sqrt(num));