class Single
{
static private int num ;
static void setNum( int nu) //求解此处为何不能 静态。明明上面num就是静态的啊
{
num = nu;
}
private Single()
{
setNum(20);
}
static private Single s = new Single();
public static Single getInstance()
{
return s;
}
static int getNum()
{
return num;
}
}
class SingleDemo
{
public static void main(String[] args)
{
Single s1 =Single.getInstance();
Single s2 = Single.getInstance();
Single s3 = Single.getInstance();//s1, s2 , s3 其实都是同一个对象。
System.out.println("num="+Single.getNum());
System.out.println("s1.num="+s1.getNum());
System.out.println("s2.num="+s2.getNum());
Single.setNum(22);
System.out.println("num="+Single.getNum());
System.out.println("s1.num="+s1.getNum());
System.out.println("s2.num="+s2.getNum());
}
}
可以使静态啊 |