单例模式

/**
单利模式的特点与写法

单利模式的类不能new,只能通过类提供的getInstance方法创建对象,而且单例模式的类的对象只有一个
@author k-ziyiqitian
*/
class People
{
private static People instance=new People();
private People(){}
public static People getInstance()
{
return instance;
}
}
class Student
{

}
public class Test
{
public static void main(String[] args)
{
People p1=People.getInstance();
People p2=People.getInstance();
System.out.println(p1==p2);
Student s1=new Student();
Student s2=new Student();
System.out.println(s1==s2);
}
}

你可能感兴趣的:(单例模式)