具有静态方法的类通常(尽管不一定)不打算被初始化。
私有构造函数可用于限制非抽象类被初始化。
比如java中的数学类。 它使构造函数标记为私有,因此您无法创建 Math 的实例。 但是 Math 类不是静态类。
这是数学课:
public final class Math {
/**
* Don't let anyone instantiate this class.
*/
private Math() {}
public static final double E = 2.7182818284590452354;
//……
public static double sin(double a) {
return StrictMath.sin(a); // default impl. delegates to StrictMath
}
//……
}
使用静态方法调用类中的静态方法时,直接使用类名.方法名即可。
例如,math.sin();
(Classes with static methods are usually (though not necessarily) not intended to be initialized.
Private constructors can be used to restrict non abstract classes from being initialized.
For example, math classes in Java. It marks the constructor private, so you cannot create an instance of math. But the math class is not static.
This is math:
public final class Math {
/**
* Don't let anyone instantiate this class.
*/
private Math() {}
public static final double E = 2.7182818284590452354;
//……
public static double sin(double a) {
return StrictMath. sin(a); // default impl. delegates to StrictMath
}
//……
}
When using static methods to call static methods in a class, the class name is used directly Method name is enough.
For example, math sin();
)
|