// 单例模式
// 一般用于工具类 ,在一个类中只能有一个对象
// Test.java
class Person {
private static Person instance = new Person();
private Person(){
//
}
public Person getInstance(){
return instance;
}
}
////////////////////////////////////////////////////////////
public class Test {
public static void main(String[] args){
Person p1=Person.getInstance();
Person p2=Person.getInstance();
System.out.println("p1 = " +p1); // p1 p2的地址是相同的
System.out.println("p2 = " + p2);
}
}