问题是 构造函数 private student(){} 运行时提示报错;求解!
class StudentDemo
{
public static void main(String[] args)
{
Student s=new Student();
s.setAge(10);
System.out.println(s.getAge());
Student1 s1=new Student1();
s1.setAge(20);
System.out.println(s1.getAge());
}
}
class Student
{
private int age;
private static Student s =new Student();
//private Single(){}
public static Student getInstance()
{
return s;
}
public void setAge(int age)
{
this.age=age;
}
public int getAge()
{
return age;
}
}
class Student1
{
private int age;
private static Student s=null;
//private Student(){}
private static Student getInstance()
{
if(s==null)
{
synchronized(Student1.class)
{
if(s==null)
s=new Student();
}
}
return s;
}
public void setAge(int age)
{
this.age=age;
}
public int getAge()
{
return age;
}
}
运行报错:private student(){} 需要返回值 |