publicclassSingletonDemo {
publicstaticinterfaceAnimal{
publicvoidsay();
}
publicstaticclassDogimplementsAnimal{
@Overridepublicvoidsay() {
System.out.println("汪汪");
}
}
publicstaticclassCatimplementsAnimal{
@Overridepublicvoidsay() {
System.out.println("喵喵");
}
}
publicstaticvoidmain(String[] args) {
Animaldog=Singleton.get(Dog.class);
Animalcat=Singleton.get(Cat.class);
System.out.println(dog==Singleton.get(Dog.class)); System.out.println(cat==Singleton.get(Cat.class)); dog.say(); cat.say(); }
}