[软件设计/软件工程] 具有静态方法的类(JAVA 中的 MATH 类)

[复制链接]
发表于 2022-5-2 11:22:41
具有静态方法的类通常(尽管不一定)不打算被初始化。

私有构造函数可用于限制非抽象类被初始化。
比如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();
)





上一篇:GC分析中提到的根对象是什么
下一篇:无效的线程间操作 PROGRESSBAR2 无法访问线程

使用道具 举报

Archiver|手机版|小黑屋|吾爱开源 |网站地图

Copyright 2011 - 2012 Lnqq.NET.All Rights Reserved( ICP备案粤ICP备14042591号-1粤ICP14042591号 )

关于本站 - 版权申明 - 侵删联系 - Ln Studio! - 广告联系

本站资源来自互联网,仅供用户测试使用,相关版权归原作者所有

快速回复 返回顶部 返回列表