黑马程序员技术交流社区

标题: 请帮忙判断下clone问题 [打印本页]

作者: 落星追恒    时间: 2016-3-20 14:36
标题: 请帮忙判断下clone问题
按照教程代码写的,但是运行一直出错不知道为啥?
public class CloneDemo {


        public static void main(String[] args) throws CloneNotSupportedException {
                Student s1 = new Student("A",13);
               
                Student s2 = new Student();
                s2 = s1;
                System.out.println(s1+"---"+s2);
               
                Student s3 = new Student();
                s3.setAge(5);
                s3.setName("sb");
                Object s4 = s3.clone();
               
                System.out.println(s3+"---"+s4);
               

        }

}
public class Student implements Cloneable {
        private String name;
        private int age;
        public Student(String name, int age) {
                super();
                this.name = name;
                this.age = age;
        }
        public Student() {
                super();
                // TODO Auto-generated constructor stub
        }
        public String getName() {
                return name;
        }
        public void setName(String name) {
                this.name = name;
        }
        public int getAge() {
                return age;
        }
        public void setAge(int age) {
                this.age = age;
        }
        @Override
        protected Object clone() throws CloneNotSupportedException {
               
                return super.clone();
        }




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2