单例模式的应用
package com.hanqi; public class Emperor { private static Emperor emperor=null; private Emperor(){ } public static Emperor getlnstance(){ if(emperor==null){ emperor=new Emperor(); } return emperor; } public void getName(){ System.out.println("我是皇帝:张卫"); } }
package com.hanqi; public class Test { public static void main(String[] args) { System.out.println("创建皇帝1对象:"); Emperor emperor1 = Emperor.getlnstance(); emperor1.getName(); System.out.println("创建皇帝2对象:"); Emperor emperor2 = Emperor.getlnstance(); emperor2.getName(); System.out.println("创建皇帝3对象:"); Emperor emperor3 = Emperor.getlnstance(); emperor3.getName(); } }