黑马程序员技术交流社区
标题:
Runtime属于单例吗?
[打印本页]
作者:
卢其龙
时间:
2012-1-5 10:59
标题:
Runtime属于单例吗?
本帖最后由 卢其龙 于 2012-1-6 04:34 编辑
Runtime属于单例类吗? 属于
public class Runtimeextends Object每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接。可以通过 getRuntime 方法获取当前运行时。
应用程序不能创建自己的 Runtime 类实例
--------------------------------------------------------------
通过gateRuntime获得实例,在内存中同时只存在一个实例.
作者:
李盈科
时间:
2012-1-5 11:04
是啊,记得看源代码很清楚的。看看下边的源代码吧!!!
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;
}
复制代码
作者:
李盈科
时间:
2012-1-5 11:04
我特别喜欢这句话:源码面前,了无秘密!
作者:
罗利民
时间:
2012-1-5 11:10
Runtime属于单例类
单例类最重要的特点是构造方法私有化,这样在类得外部就不能构造和创建类得实例。
作者:
代臣
时间:
2012-1-5 12:05
判断Runtime类是否用到了单例模式的条件:
1.在Runtime类中是否定义了private Runtime(){}构造函数;
2.在Runtime类中是否创建了私有(private)并静态(static)的内部对象:比如private static Runtime R = new Runtime();
3.在Runtime类中是否定义了一个public、static的返回值类型为Runtime类型的获取对象信息的方法。
就知道这么多,希望对你有用。
作者:
林晓波
时间:
2012-1-5 18:22
Runtime对象
该类并没有提供构造函数。
说明不可以new对象。那么会直接想到该类中的方法都是静态的。
发现该类中还有非静态方法。
说明该类肯定会提供了方法获取本类对象。而且该方法是静态的,并返回值类型是本类类型。
由这个特点可以看出该类使用了单例设计模式完成。
该方式是static Runtime getRuntime();
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2