【JAVA 课后习题 13.11】+ 接口

水题~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()));
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值