public class Test1 {
public static void main(String[] args) {
funding f1 =funding.getInstance();
f1.print();
System.out.println(f1);
funding f2=funding.getInstance();
f2.print();
System.out.println(f2);
}
}
//单例模式
class funding{
private static final funding f=new funding();//这样写,所有的类的对象都占用同一份空间
private funding(){
}
public void print(){
System.out.println("funding:100$");
}
public static funding getInstance(){
return f;
}
}
==================================================================
/*class funding{
private funding(){
}
public void print(){
System.out.println("funding:100$");
}
public static funding getInstance(){
return new funding();//这样写,所有对象不占用同一份空间,只是外部不能随便使用
}
}*/