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