class Book implements Cloneable{
private String title;
private double price;
public Book(String title, double price) {
this.title = title;
this.price = price;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return "书名《" + this.title + "》价格:" + this.price + "元。";
}
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
public class Demo {
public static void main(String[] args) throws Exception {
Book booka = new Book("Java开发", 88.6);
Book bookb = (Book) booka.clone();
bookb.setTitle("Android开发");
System.out.println(booka);
System.out.println(bookb);
}
}
Java 基础类库(对象克隆)
最新推荐文章于 2024-08-15 14:47:41 发布