以下代码怎么说明延迟加载和单例设计模式
public class Test8{
static class People{
private String name;
private String sex;
private int age;
private People(String name,String sex,int age){
super();
this.name=name;
this.sex=sex;
this.age=age;
}
public static People p=null;
public static People getPeople(){
synchronized (People.class){
if(p==null){
p=new People("zhangfan","女",22);
}
}
return p;
}
public String toString() {
return "姓名:"+name+" 年龄: "+age;
}
}
public static void main(String[] args) {
People p1=People.getPeople();
People p2=People.getPeople();
People p3=People.getPeople();
System.out.println(p1);
System.out.println(p2);
System.out.println(p3);
}
} |