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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 卢其龙 黑马帝   /  2012-1-5 10:59  /  2519 人查看  /  7 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 卢其龙 于 2012-1-6 04:34 编辑

Runtime属于单例类吗?  属于



public class Runtimeextends Object每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。可以通过 getRuntime 方法获取当前运行时。

应用程序不能创建自己的 Runtime 类实例
--------------------------------------------------------------
通过gateRuntime获得实例,在内存中同时只存在一个实例.

评分

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

查看全部评分

7 个回复

倒序浏览
是啊,记得看源代码很清楚的。看看下边的源代码吧!!!
  1. public class Runtime {
  2.     private static Runtime currentRuntime = new Runtime();

  3.     /**
  4.      * Returns the runtime object associated with the current Java application.
  5.      * Most of the methods of class <code>Runtime</code> are instance
  6.      * methods and must be invoked with respect to the current runtime object.
  7.      *
  8.      * @return  the <code>Runtime</code> object associated with the current
  9.      *          Java application.
  10.      */
  11.     public static Runtime getRuntime() {
  12.         return currentRuntime;
  13.     }
复制代码
回复 使用道具 举报
我特别喜欢这句话:源码面前,了无秘密!

评分

参与人数 1技术分 +1 收起 理由
admin + 1 赞一个!

查看全部评分

回复 使用道具 举报
Runtime属于单例类
单例类最重要的特点是构造方法私有化,这样在类得外部就不能构造和创建类得实例。

评分

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

查看全部评分

回复 使用道具 举报
判断Runtime类是否用到了单例模式的条件:
1.在Runtime类中是否定义了private Runtime(){}构造函数;
2.在Runtime类中是否创建了私有(private)并静态(static)的内部对象:比如private static Runtime R = new Runtime();
3.在Runtime类中是否定义了一个public、static的返回值类型为Runtime类型的获取对象信息的方法。
就知道这么多,希望对你有用。

评分

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

查看全部评分

回复 使用道具 举报
Runtime对象
该类并没有提供构造函数。
说明不可以new对象。那么会直接想到该类中的方法都是静态的。
发现该类中还有非静态方法。
说明该类肯定会提供了方法获取本类对象。而且该方法是静态的,并返回值类型是本类类型。
由这个特点可以看出该类使用了单例设计模式完成。
该方式是static Runtime getRuntime();

评分

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

查看全部评分

回复 使用道具 举报
好像还有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() {}   
你要从实例入手呢   你先看看代码  不懂的话 你再问问  
回复 使用道具 举报
你可以Runtime的源代码就可以了。Runtime的构造函数是私有的,从程序角度它是不允许你直接创建。如果你的Runtime可以随你创立,那JAVA程序的安全性将会大为降低。

评分

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

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马