不是说静态只能访问静态吗,下面的代码中,s1是静态的而setName()和getName都是非静态的为什么可以访问呢?
class Student
{
private Student(){}
private String name;
private int age;
private static Student s = new Student();
public static Student getStudent()
{
return s;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return this.name;
}
}
class StudentDemo
{
public static void main(String[] args)
{
Student s1 = Student.getStudent();
s1.setName("lisi");
System.out.println(s1.getName());
}
} |