水题~clone + dompareto 接口实现~~这种水题~还敲了1个多小时~还是太菜~还不及时练习~附上本菜鸡13.12的实现代码~~
GeOb类实现代码(其实跟本题clone + dompareto 接口实现没太大关系=.=) :
public abstract class GeOb {
private String color = "white";
private boolean filled;
protected java.util.Date da;
protected GeOb(){
da = new java.util.Date();
}
protected GeOb(String color,boolean filled){
da = new java.util.Date();
this.color = color;
this.filled = filled;
}
public String getcolor(){
return color;
}
public void setcolor(String color){
this.color = color;
}
public boolean getfillrd(){
return filled;
}
public void setfilled(boolean filled){
this.filled = filled;
}
public java.util.Date getda(){
return da;
}
public String toString(){
return "created on " + da + "\ncolor : " + color + " and filled : " + filled;
}
public abstract double getArea();
public abstract double getPerimeter();
}
clone + dompareto 接口实现~~(这里附上 非深入复制 + 深入复制)
非深入复制~~
public class Octogon extends GeOb implements Comparable<Octogon>,Cloneable{
private int b;
public Octogon(int b){
this.b = b;
}
public void setOc(int b){
this.b = b;
}
public int compareTo(Octogon o) {
if(o.getArea() > this.getArea())
return 1;
if(o.getArea() < this.getArea())
return -1;
else
return 0;
}
public double getArea() {
return (2 + 4 * Math.sqrt(2.0) * this.b * this.b);
}
public double getPerimeter() {
return 8 * this.b;
}
public Object clone() throws CloneNotSupportedException{
return super.clone();
}
}
深入复制~~
public class Octogon extends GeOb implements Comparable<Octogon>,Cloneable{
private int b;
public Octogon(int b){
this.b = b;
}
public void setOc(int b){
this.b = b;
}
public int compareTo(Octogon o) {
if(o.getArea() > this.getArea())
return 1;
if(o.getArea() < this.getArea())
return -1;
else
return 0;
}
public double getArea() {
return (2 + 4 * Math.sqrt(2.0) * this.b * this.b);
}
public double getPerimeter() {
return 8 * this.b;
}
public Object clone() throws CloneNotSupportedException{
Octogon d = (Octogon)super.clone();
d.da = (java.util.Date)(da.clone());
return d;
}
}
Text 实现代码 (即对前面类的实现,看心情怎么开心怎么谢咯~~)
public class Text {
public static void main(String[] args) throws CloneNotSupportedException {
Octogon a = new Octogon(5);
System.out.println("The Area is : " + a.getArea());
System.out.println("The Perimeter is : " + a.getPerimeter());
Octogon b = (Octogon)a.clone();
System.out.println("a = b ? " + (a == b));
System.out.println("a.getArea() = b.getArea() ? " + (a.getArea() == b.getArea()));
System.out.println("a.getPerimeter() = b.getPerimeter() ? " + (a.getPerimeter() == b.getPerimeter()));
System.out.println("a.getda() = b.getda() ? " + (a.getda() == b.getda()));
System.out.println("a.getcolor() = b.getcolor() ? " + (a.getcolor() == b.getcolor()));
}
}