- public class Danli {
- public static void main(String[] args) throws IOException {
- Sing s1 = Sing.getInstance();
- s1.show();
- Sing s2 = Sing.getInstance();
- s1.show();
- }
- }
- class Sing{
- private Sing(){
- }
- private static Sing s = new Sing();
- public static Sing getInstance(){
- return s;
- }
- public void show(){
- System.out.println("hehe");
- }
- }
复制代码 单例模式只能创建一个实例对象,但是如上所示,当两次调用Sing的时候,是创建了多少个对象呢 |
|