带有静态方法的类(java中的math类)

带有静态方法的类通常(虽然不一定是这样)不打算被初始化。

可以用私有构造函数来限制非抽象类被初始化。

例如,java中的math类。它让构造函数标记为私有,所以你无法创建Math的实例。但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();


原文地址:https://www.cnblogs.com/jiangu66/p/3228622.html