2015-06-03
原CSDN博客已弃用,文章会逐渐迁移过来。
应朋友们反馈的JAVA基础薄弱的问题,决定出一套完整的JAVA基础教程,帮助大家复习,巩固JAVA基础。
图书馆类
1、被克隆的类需要实现Cloneable接口,重写Object的clone方法。
public class TuShuGuan implements Cloneable{
private String shuming;
private String time;
public TuShuGuan(String shuming, String time) {
super();
this.shuming = shuming;
this.time = time;
}
public void setShuming(String shuming) {
this.shuming = shuming;
}
public void setTime(String time) {
this.time = time;
}
public String getShuming() {
return shuming;
}
public String getTime() {
return time;
}
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
主函数
主函数需要抛CloneNotSupportedException异常
/**
* Java中的clone
* @author Administrator
*
*/
public class CloneTest {
public static void main(String[] args)throws CloneNotSupportedException{
TuShuGuan t = new TuShuGuan("java","2017年");
System.out.println(t.getShuming()+"---"+t.getTime());
Object obj = t.clone();
TuShuGuan t2 = (TuShuGuan)obj;
System.out.println(t2.getShuming()+"---"+t2.getTime());
TuShuGuan t3 = t;
t3.setShuming("C++");
t3.setTime("2018年");
System.out.println(t3.getShuming()+"---"+t3.getTime());
System.out.println(t2.getShuming()+"---"+t2.getTime());
}
}