A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 落星追恒 中级黑马   /  2016-3-20 14:36  /  305 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

按照教程代码写的,但是运行一直出错不知道为啥?
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();
        }

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马