因为你没有覆盖Student类中的toString()方法,所以,它默认调用super()父类中的toString()方法
- /*
- 面向对象
- 单例设计模式
- */
- class SingleDemo
- {
-
- public static void main(String[] args)
- {
- Student s1=Student.getStudent();
- s1.setAge(15);
- System.out.println(s1);
- }
-
- }
- class Student
- {
- private int age;
- private static Student s=new Student();
- private Student(){}
- public static Student getStudent()
- {
- return s;
- }
- public void setAge(int age)
- {
- this.age=age;
- }
- public int getAge()
- {
- return age;
- }
- @Override
- public String toString() {
- return "Student [age=" + age + "]";
- }
-
-
- }
复制代码 |