克隆在JAVA中的用途是什么?经常使用它?

时间:2016-05-28 17:59:12

标签: java oop clone abstract comparable

我使用过编程并且理解克隆意味着对象的重复。但是,我无法了解在什么情况下以及它主要用于何处?我的意思是,它似乎在哪里使用?

public abstract class GeometricObject { 
    private String color = "white";
    private boolean filled;

    //Default constructor
    protected GeometricObject(){

    }
    //Constructing a Geometric object
    protected GeometricObject(String color, boolean filled){
        this.color = color;
        this.filled = filled;
    }
    //Getter method for color
    public String getColor(){
        return color;
    }
    //set method for color
    public void setColor(String color){
        this.color = color;
    }
    //Getter method for filled
    public boolean isFilled(){
        return filled;
    }
    //setter method for filled
    public void setFilled(boolean filled){
        this.filled = filled;
    }
    //Abstract method for getting Area
    public abstract double getArea();

    //Abstract method for getting perimeter
    public abstract double getPerimeter();

}

public class Octagon extends GeometricObject implements Comparable<Octagon> , Cloneable {
    private double sides;
    //Default constructor
    public Octagon(){

    }

    /**New Octagon Object**/
    public Octagon(double side){
        this.sides = side;
    }

    /**Getter method**/
    public double getSide(){
        return sides;
    }

    /**Setter method**/
    public void setSide(double side){
        this.sides = side;
    }


    @Override
    public double getArea() {
        double area = (2*(1+(Math.sqrt(2)))*sides*sides);
        // TODO Auto-generated method stub
        return area;
    }

    @Override
    public double getPerimeter() {
        double perimeter = sides * 8;
        return perimeter;
    }

    @Override
    public int compareTo(Octagon o) {
        if(getArea()>o.getArea())
            return 1;
        else if(getArea()<o.getArea())
            return -1;
        else 
            return 0;
    }
    @Override
     public Object clone() {
            try {
              return super.clone();
            }
            catch (CloneNotSupportedException ex) {
              return null;
            }
          }
}

public class Test {

    public static void main(String[] args) {
        /**Creating a new Ocatgon object of having side value 5**/
        Octagon oct = new Octagon(7);
        //getting Area of new octagon
        System.out.println("The area of Octagon with side 5.0 is (A): "+ oct.getArea());
        /**Getting perimeter of new Octagon**/
        System.out.println("The perimeter of Octagon with side 5.0 is (P): "+ oct.getPerimeter());
        /*
         * Creating a new object using clone method and 
         * copy of oct whose side is 5
        */
        Octagon octagon1 = (Octagon)oct.clone();

        /*
         * comparing the two objects i.e.  using compareTo method.
         */
        int i= oct.compareTo(octagon1);
        if(i<0){
            System.out.println("Clone Octagon is grater than original octagon");
        }else if(i>0)
            System.out.println("Clone octagon is smaller than original octagon");
        else
            System.out.println("Clone octagon is Equal to original octagon");   
    }
}

1 个答案:

答案 0 :(得分:0)

clone object你的类应该实现Cloneable接口,你应该从对象类覆盖clone方法,这很奇怪,克隆方法应该在Cloneable上界面。您在Clone方法上的实现和使用克隆对象是正确的。我假设你没有得到概念Duplication of objects。例如

你有一个八角形物Octagon octagon1 = new Octagen();;

你引用了Octagon octagon2 = octagon1;

即使octagon1octagon2是两个不同的变量,它们也会保持对同一个对象的引用。如果使用任何变量对对象进行更改,则更改将影响2个变量,因为它们引用同一对象。

octagon2.setSide(2);
octagon1.getSide(); //will return 2.

通过克隆您赢得的对象而不会遇到上述问题。

 Octagon oct = new Octagon(7);
 Octagon octagon1 = (Octagon)oct.clone();

octagon1.setSide(3);
oct.getSide(); //will not return 3 because they are not referring to the same object.