class Person {
public void test(){
Singleton singleton = Singleton.getInstance();
System.out.println(singleton.getAge());
}
}
class Singleton {
private int age;
private String name;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private static Singleton instance = null;
private Singleton(){
}
public static Singleton getInstance(){
if(instance == null){
instance = new Singleton();
}
return instance;
}
}
public class Main {
public static void main(String[] args) {
Singleton singleton = Singleton.getInstance();
singleton.setAge(12);
Person person = new Person();
person.test();
}
}
08-16
1173
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-18
340
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-19
717
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)