package Cloneable;
class Student implements Cloneable
{
String name;
int age;
Father f;
int[] a = {1,2,3};
Book[] books;
Student(String name,int age,Father f,Book[] books)
{
this.name = name;
this.age = age;
this.f = f;
this.books = books;
}
public String toString()
{
String book = "";
for(Book b:books)
{
book+=b;
}
return "名字:"+name+" 年龄:"+age+" "+f+" "+book;
}
public Student clone() throws CloneNotSupportedException
{
Student s = (Student)super.clone();
s.f = this.f.clone();
s.a = this.a.clone();
s.books = this.books.clone();//克隆数组的引用
for(int i=0;i<books.length;i++)//克隆数组里面具体的内容
{
s.books[i] = this.books[i].clone();
}
return s;
}
};
class Father implements Cloneable
{
int age;
String name;
Father(String name,int age)
{
this.name = name;
this.age = age;
}
public String toString()
{
return "父亲名:"+name+" 父亲年龄:"+age;
}
public Father clone() throws CloneNotSupportedException
{
return (Father)super.clone();
}
};
class Book implements Cloneable
{
String name;
Book(String name)
{
this.name = name;
}
public String toString()
{
return "书名:"+name;
}
public Book clone() throws CloneNotSupportedException
{
return (Book)super.clone();
}
};
class CloneTest
{
public static void main(String[] args) throws CloneNotSupportedException
{
Father f = new Father("liyi",45);
Book[] books = {new Book("java"),new Book("C语言"),new Book("web-1")};
Student s1 = new Student("lili",25,f,books);
Student s2 = s1.clone();
System.out.println(s1);
System.out.println(s2);
System.out.println(s1==s2);
s1.name = "lucy";
System.out.println(s1);
System.out.println(s2);
s1.f.name = "Micro";
System.out.println(s1);
System.out.println(s2);
s1.a[2] = 100;
System.out.println(s2.a[2]);
System.out.println(s1.books==s2.books);
s1.books[0].name = "神雕侠侣";
System.out.println(s1);
System.out.println(s2);
}
};
package clone;
class Students2 implements Cloneable//实现Cloneable接口表示可以被克隆
{
String name;
int age;
int []a={1,2,3,4,5};
Father f=new Father("java",80);
Students2(String name,int age)
{
this.name=name;
this.age=age;
}
public Students2 clone()throws CloneNotSupportedException
{
return (Students2) super.clone();//具体的克隆操作由父类来完成
}
public String toString()//重写toString()方法
{
return "姓名:"+name+"年龄:"+age;
}
}
class Father implements Cloneable//实现Cloneable接口表示可以被克隆
{
String name;
int age;
Father(String name,int age)
{
this.name=name;
this.age=age;
}
public Father clone()throws CloneNotSupportedException
{
return (Father) super.clone();
}
public String toString()
{
return "姓名:"+name+"年龄:"+age;
}
}
class StudentTest1
{
public static void main(String[] args) throws CloneNotSupportedException
{
Students2 s1=new Students2("lili",25);
Students2 s2=s1.clone();
System.out.println("对象的克隆:");
System.out.println(s1);
System.out.println(s2);
int []b=s1.a.clone();
System.out.println("数组的克隆:");
System.out.println("原始的数组:");
for(int i:s1.a)
{
System.out.print(i+" ");
}
System.out.println("\n克隆后的数组:");
for(int i:b)
{
System.out.print(i+" ");
}
System.out.println("\n类的克隆:");
System.out.println("原始对象:");
System.out.println(s1.f);
Father ff=s1.f.clone();
System.out.println("克隆后的对象:");
System.out.println(ff);
System.out.println("克隆后重新为原对象赋值,看看克隆的对象与被克隆的对象的值是否发生改变:");
for(int i=0;i<s1.a.length;i++)
{
s1.a[i]=5-i;
}
System.out.println("\n重新为a赋值后,a数组的内容变为:");
for(int i:s1.a)
{
System.out.print(i+" ");
}
System.out.println("\n但是b克隆a的内容还是:");
for(int i:b)
{
System.out.print(i+" ");
}
System.out.println("\n重新为Fathe f赋值后,f的内容变为:");
s1.f=new Father("Java SE",100);
System.out.println(s1.f);
System.out.println("为F赋值后,但是克隆f的对象ff的内容还是:");
System.out.println(ff);
}
}