导入Math.PI作为参考或值 [英] Importing Math.PI as reference or value
问题描述
我正准备用Java进行基本认证。
I'm preparing for a basic certification in Java.
我对一个我正确的问题(!)的答案感到有些困惑: -
I'm a little confused by the answer to a question that I have got right(!):-
鉴于:
public class Circle {
static double getCircumference(double radius ) {
return PI * 2 * radius;
}
public static double getArea(double radius) {
return PI * radius * radius;
}
}
哪个import语句将使代码能够编译和运行?
Which import statement will enable the code to compile and run?
import java.lang.*;
import static java.lang.Math.PI;
import java.lang.Math.*;
import java.lang.Math;
我回答了import static java.lang.Math.PI;
I answered import static java.lang.Math.PI;
但下面另外两个选项的解释让我感到困惑: -
BUT the explanation of two other options below confuses me:-
语句导入java.lang.Math ;并导入java.lang.Math。*;不会启用代码编译和运行。这些import语句只允许Math.PI作为PI常量的引用。
The statements import java.lang.Math; and import java.lang.Math.*; will not enable the code to compile and run. These import statements will only allow Math.PI as a reference to the PI constant.
我的问题是:import语句只会允许引用PI常数?该值是否未初始化为零?
My question is: what would be wrong with the import statements only allowing a reference to the PI constant? Would the value be uninitialized and zero?
推荐答案
'允许Math.PI作为PI常量的引用'表示您的代码必须看起来像这样才能工作:
'Allow Math.PI as a reference to the PI constant' means that your code will have to look like this in order to work:
static double getCircumference(double radius ) {
return Math.PI * 2 * radius;
}
public static double getArea(double radius) {
return Math.PI * radius * radius;
}
什么 import java.lang.Math;
确实导入了类 java.lang.Math
,所以你可以用 Math
来代替限定版本 java.lang.Math
。 import java.lang.Math。*;
对 Math
和所有嵌套类执行相同操作,但不是它的成员。
What import java.lang.Math;
does is importing the class java.lang.Math
so you can reference it with Math
instead of the qualified version java.lang.Math
. import java.lang.Math.*;
does the same for Math
and all nested classes, but not it's members.
这篇关于导入Math.PI作为参考或值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!