懒汉式单例
class Person{
static Person per =null;
private Person(){};
public static Person aa()
{
if(per == null)
per= new Person();
return per;
}
}
class wuciqiu{
public static void main(String [] args)
{
Person per1=Person.aa();
Person per2=Person.aa();
if(per1 == per2)
System.out.println("per1 == per2");
else System.out.println("per1 != per2");
}
}
饿汉式单例
class Person{
static Person per =new Person();
private Person(){};
public static Person aa()
{
return per;
}
}
class wuciqiu{
public static void main(String [] args)
{
Person per1=Person.aa();
Person per2=Person.aa();
if(per1 == per2)
System.out.println("per1 == per2");
else System.out.println("per1 != per2");
}
}