Aplicación de patrón singleton en el JDK

Hombre de pocas palabras, dijo, directamente en el código fuente

El uso de un patrón singleton de la clase en tiempo de ejecución:

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.
     */
     //在方法中调用创建好的静态方法currentRuntime
    public static Runtime getRuntime() {
        return currentRuntime;
    }

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

Publicado 47 artículos originales · ganado elogios 34 · vistas 8856

Supongo que te gusta

Origin blog.csdn.net/weixin_42893085/article/details/105413853
Recomendado
Clasificación