A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 林枢 黑马帝   /  2011-7-26 22:10  /  2622 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

只能getRuntime

评分

参与人数 1技术分 +1 收起 理由
admin + 1

查看全部评分

3 个回复

倒序浏览
黑马网友  发表于 2011-7-26 22:22:22
沙发
单例模式,你看看Runtime的源代码就可以了。
Runtime的构造函数是私有的,从程序角度也不允许你直接创建。
从深层次考虑,如果你的Runtime可以随你创立,那JAVA程序的安全性将会大为降低。

建议你多了解点单例模式。类似的还有Calendar.getInstance()

public class Runtime {
    private static Runtime currentRuntime = new Runtime();

    /**
     * Returns the runtime object associated with the current Java application.
     * Most of the methods of class <code>Runtime</code> are instance
     * methods and must be invoked with respect to the current runtime object.
     *
     * @return  the <code>Runtime</code> object associated with the current
     *          Java application.
     */
    public static Runtime getRuntime() {
return currentRuntime;
    }

    /** Don't let anyone else instantiate this class */
    private Runtime() {}

评分

参与人数 1技术分 +2 收起 理由
admin + 2 辛苦了!

查看全部评分

回复 使用道具 举报
黑马网友  发表于 2011-7-27 18:54:10
藤椅

回复 楼主 的帖子

这是一个单例模式,Runtime的构造函数是private,那是不能new的
回复 使用道具 举报
黑马网友  发表于 2011-7-27 22:57:54
板凳
Runtime类封装了运行时的环境。每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。既然是运行时类实例,一旦程序运行就说明已经有这个实例对象了为啥还要再new实例对象呢.
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马