1 //单例模式---懒汉模式 2 public class Apple{ 3 //创建一个成员,在内存中只有一个拷贝 4 private static Apple apple = null; 5 private Apple(){ 6 7 } 8 //这个方法用来创建实例 9 public static Apple Instance(){ 10 if(appel == null){ 11 Apple apple = new Apple(); 12 } 13 return apple; 14 } 15 } 16 17 18 19 //单例模式---饿汉模式 20 public class Apple2(){ 21 private static Apple2 apple2 = new Apple(); 22 private Apple2(){ 23 24 } 25 public static Apple2 getInstance(){ 26 return apple2; 27 } 28 }