package cn.itcast_06;
import java.io.IOException;
/*
*
* Runtime:每个Java应用程序都有一个Runtime类实例,使应用程序与其运行环境连接
* exec(String name)
* 调取密令
*
*/
public class 单例模式的Java代码体现Runtimo {
public static void main(String[] args) throws IOException {
Runtime r = Runtime.getRuntime();
//r.exec("winmine");
//r.exec("notepad");//打开记事本
//r.exec("calc");//打开计算器
//r.exec("shutdown -s -t 10000");//关机密令
//r.exec("shoutdown -a");//取消关机密令
}
}
/*
*
*class Runtime{
* private Runtime(){}
* private static Runtime currenRuntime = new Runtime();
* public static Runtime getRuntime(){
* return currenRuntime;
* }
* }
*
*
*
*/
单例模式的Java代码体现Runtimo
该博客探讨了Java中的Runtime类,指出每个Java应用程序都拥有一个Runtime实例,用于与运行环境交互。示例代码展示了如何使用Runtime.exec()方法执行系统命令,如打开记事本、计算器,甚至执行关机和取消关机命令。文章通过单例模式来表示Runtime类的私有化构造函数和静态获取当前实例的方法。

被折叠的 条评论
为什么被折叠?



