标题: 求助 [打印本页] 作者: 王春祥 时间: 2012-3-19 14:56 标题: 求助 class Single
{
private int num;
private Single(){}
private static Single s=null;
public static Single getInstance()
{
if(s==null)
s=new Single();
return s;
}
public void setNum(int num)
{
this.num=num;
}
public int getNum()
{
return num;
}
class SingleDemo
{
public static void main(String[] args)
{
Single s1=Single.getInstance();
s1.setNum(20);
System.out.println("num="+s1.getNum());
}
}
SingleDemo.java:56: 错误: 进行语法分析时已到达文件结尾 为什么呢,把private static Single s=null;放类体第一行就行了?
作者: 李深山 时间: 2012-3-19 15:04
class Single
{
private int num;
private Single(){}
private static Single s=null;
public static Single getInstance()
{
if(s==null)
s=new Single();
return s;
}
public void setNum(int num)
{
this.num=num;
}
public int getNum()
{
return num;
}}//你这里少了一个花括号
class SingleDemo
{
public static void main(String[] args)
{
Single s1=Single.getInstance();
s1.setNum(20);
System.out.println("num="+s1.getNum());