本帖最后由 袁錦泰 于 2012-5-19 22:02 编辑
- public class Student {
- private String name;
- private int age;
- private Student() {
- }
- private static Student s = new Student();
- public static Student getInstance() {
- return s;
- }
- public void setName(String name) {
- this.name = name;
- }
- public String getName() {
- return name;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public int getAge() {
- return age;
- }
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Student s1 = Student.getInstance();
- s1.setName("zhangsan");
- System.out.println(s1.getName());
- s1.setAge(20);
- System.out.println(s1.getAge());
- Student s2 = new Student();
- s2.setName("lisi");
- System.out.println(s2.getName());
- s2.setAge(21);
- System.out.println(s2.getAge());
- }
- }
复制代码 帮忙分析一下我哪里写错了,运行竟然可以分别输出两个人的姓名和年龄 |