为什么实现了Cloneable接口还是会报错啊?
//Person类
public class Person implements Cloneable{
private String phoneNumber;
private String Name;
public void setPhoneNumber(String number){
phoneNumber=number;
}
public String getPhoneNumber(){
return phoneNumber;
}
public void setName(String name){
Name=name;
}
public String getName(){
return Name;
}
public Person(){
}
public Person(String number,String name){
this.phoneNumber=number;
this.Name=name;
}
public String toString(){
return "Person类:"+"姓名为"+getName()+" 电话为"+getPhoneNumber();
}
public Object clone(){
try{
return super.clone();
}
catch(CloneNotSupportedException ex){
return null;
}
}
}
//Person的子类Student
public class Student extends Person implements Cloneable{
final static int FIRSTGRADE=1;
final static int SECONDGRADE=2;
final static int THIRDGRADE=3;
final static int FOURTHYEAR=4;
private int Grade;
public Student(){
}
public Student(String number,String name,int grade){
this.setPhoneNumber(number);
this.setName(name);
this.Grade=grade;
}
public void setGrade(int grade){
Grade=grade;
}
public int getGrade(){
return Grade;
}
public String toString(){
return "Student类:"+"姓名为"+getName()+" 电话为"+getPhoneNumber()+" 年级为大学"+getGrade()+"年级";
}
public String displayObject(){
return "to "+getName()+":请按时提交实验报告!";
}
public Object clone(){
return super.clone();
}
}
//Course类
import java.util.ArrayList;
public class Course implements Cloneable{
private String courseName;
private ArrayList<Student> student;
private int numberOfStudents;
public Course(String courseName){
student=new ArrayList();
this.courseName=courseName;
}
public void addStudent(Student newStudent){
student.add(newStudent);
numberOfStudents++;
}
public String getCourseName() {
return courseName;
}
public ArrayList<Student> getStudent() {
return student;
}
public int getNumberOfStudents() {
return numberOfStudents;
}
public void dropStudent(Student aStudent){
student.remove(aStudent);
numberOfStudents--;
}
public Object clone(){
try{
Course course=(Course)super.clone();
course.student=(ArrayList<Student>)student.clone();
return course;
}
catch(CloneNotSupportedException ex){
return null;
}
}
public static void main(String[] args) throws CloneNotSupportedException {
Course c1=new Course("SSD3-Java");
Student s1=new Student("001","LiHua",3);
c1.addStudent(s1);
Course c2=(Course)(c1.clone());
System.out.println(c1.getStudent()==c2.getStudent());
System.out.println(c1.getStudent().equals(c2.getStudent()));
}
}