JAVA中的Cloning有啥用,经常用到哪里?

Posted

技术标签:

【中文标题】JAVA中的Cloning有啥用,经常用到哪里?【英文标题】:What is the use of Cloning in JAVA and where it is frequently used?JAVA中的Cloning有什么用,经常用到哪里? 【发布时间】:2016-09-26 21:35:09 【问题描述】:

我使用过编程并理解克隆意味着复制对象。但是,我不知道它主要在什么背景下使用以及在哪里使用?我的意思是,它似乎在哪里使用?

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");   
    

【问题讨论】:

你的代码和这个问题有什么关系? 我自己完成了这个编程。我要问的问题是,克隆主要在哪里使用。只是没有看到代码。举个例子说明你为什么使用克隆? why we use clone() method in java?的可能重复 您能解释一下为什么您(以及其他许多人)称它为 JAVA,而它的名字实际上是,真的,老实说,认真,而不是首字母缩略词? clone()的使用示例见javadoc:Uses of Interface java.lang.Cloneable 【参考方案1】:

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

你有一个八角形的对象Octagon octagon1 = new Octagen();;

你有一个Octagon octagon2 = octagon1;的引用

即使 octagon1octagon2 是 2 个不同的变量,它们仍保持对同一对象的引用。如果您使用任何变量对对象进行更改,则更改将影响这 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.

【讨论】:

这意味着对象将被复制而不会干扰原始对象。我希望我现在明白了。非常感谢老兄!!

以上是关于JAVA中的Cloning有啥用,经常用到哪里?的主要内容,如果未能解决你的问题,请参考以下文章

java中io主要有啥用,重要不

java中的jar包有啥用

C语言中的枚举类型有啥用么?

java中的StringBuilder有啥用?啥时候用StringBuilder?

Java中的自动行刷新,加不加true有啥区别?不加true也照样换行吧?这个true有啥用?

java token有啥用