通过导入 Java 中的数学类方法
静态导入是指如果字段和方法在类中被定义为公共静态,则可以在代码中使用它们,而不必指定它们的类。
java.lang 包中的 Math 类方法 sqrt() 是静态导入的。
演示这一点的一个程序如下所示
示例
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));
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程语言
C++
C#
MongoDB
MySQL
Javascript
PHP