单例模式下的嵌套调用导致栈溢出

单例模式下的嵌套调用导致栈溢出

package com.asyntask;

import android.util.Log;

public class Demo2 {
    public void print() {
        Demo1.getInstance().print();
        Log.d("Demo2", "demo2");
    }
}

package com.asyntask;

import android.util.Log;

public class Demo1 {
private static Demo1 mDemo1;
private Demo2 mDemo2;
public static Demo1 getInstance(){
if(mDemo1 == null){
mDemo1 = new Demo1();
}
return mDemo1;
}

public void print(){
    Log.d("Demo1", "Demo1");
}

public Demo1() {
    mDemo2 = new Demo2();
    mDemo2.print();
}

}

package com.asyntask;

import android.util.Log;

public class Demo2 {
    public void print() {
        Demo1.getInstance().print();
        Log.d("Demo2", "demo2");
    }
}

写代码过程犯的错误,记录下以后避免

你可能感兴趣的:(栈)