在Java类Math中为sqrt()和pow()方法使用静态导入
静态导入意味着如果将类中的字段和方法定义为公共静态,则无需指定其类即可在代码中使用它们。
Math类方法sqrt()以及pow()java.lang包中的方法都是静态导入的。演示此过程的程序如下:
示例
import static java.lang.Math.sqrt;
import static java.lang.Math.pow;
public class Demo {
public static void main(String args[]) {
double num = 4.0;
System.out.println("The number is: " + num);
System.out.println("The square root of the above number is: " + sqrt(num));
System.out.println("The square of the above number is: " + pow(num, 2));
}
}输出结果
The number is: 4.0 The square root of the above number is: 2.0 The square of the above number is: 16.0
现在让我们了解上面的程序。
方法不需要Math类sqrt(),pow()因为java.lang包使用静态导入。显示数字num及其平方根和平方。演示此代码段如下:
double num = 4.0;
System.out.println("The number is: " + num);
System.out.println("The square root of the above number is: " + sqrt(num));
System.out.println("The square of the above number is: " + pow(num, 2));热门推荐
1 狗狗技能训练
5 如何训练狗狗笑
6 狗狗训练排便
9 狗狗响片训练
10 如何训练狗狗不捡食
11 狗狗尿训练
12 怎么训练狗狗找东西
13 狗狗训练站立
14 狗狗训练飞盘
15 鞍山狗狗训练
16 怎样训练泰迪狗狗谢谢
17 狗狗外出训练
18 怎么训练金毛狗狗大小便