Java 中的静态导入、注解与泛型基础
静态导入
在 Java 里, import 关键字有了更广泛的用途。在 import 后面加上 static 关键字, import 语句就能用来导入类或接口的静态成员,这就是静态导入。使用静态导入时,可以直接通过静态成员的名称来引用它们,而无需用类名来限定,这简化并缩短了使用静态成员所需的语法。
为了理解静态导入的实用性,先看一个未使用静态导入的例子。以下程序用于计算二次方程 $ax^2 + bx + c = 0$ 的解:
// Find the solutions to a quadratic equation.
class Quadratic {
public static void main(String args[]) {
// a, b, and c represent the coefficients in the
// quadratic equation: ax2 + bx + c = 0
double a, b, c, x;
// Solve 4x2 + x - 3 = 0 for x.
a = 4;
b = 1;
c = -3;
// Find first solution.
x = (-b + Math.sqrt(Math.pow(b, 2) - 4 * a * c))
超级会员免费看
订阅专栏 解锁全文
16万+

被折叠的 条评论
为什么被折叠?



