Cloning 在 JAVA 中有什么用,经常用在什么地方?
What is the use of Cloning in JAVA and where it is frequently used?
我用过编程,明白克隆就是对象的复制。但是,我不知道它主要在什么情况下以及在哪里使用?我的意思是,它似乎在哪里使用?
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");
}
}
到 clone
一个 object
你的 class 应该实现 Cloneable
接口并且你应该覆盖对象 class 的 clone
方法奇怪,克隆方法应该在 Cloneable 接口上。您对 Clone 方法和使用克隆对象的实现是正确的。我假设您没有理解 Duplication of objects
的概念。举个例子
你有一个八边形的对象Octagon octagon1 = new Octagen();;
并且您有 Octagon octagon2 = octagon1;
的引用
尽管 octagon1
和 octagon2
是 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.
我用过编程,明白克隆就是对象的复制。但是,我不知道它主要在什么情况下以及在哪里使用?我的意思是,它似乎在哪里使用?
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");
}
}
到 clone
一个 object
你的 class 应该实现 Cloneable
接口并且你应该覆盖对象 class 的 clone
方法奇怪,克隆方法应该在 Cloneable 接口上。您对 Clone 方法和使用克隆对象的实现是正确的。我假设您没有理解 Duplication of objects
的概念。举个例子
你有一个八边形的对象Octagon octagon1 = new Octagen();;
并且您有 Octagon octagon2 = octagon1;
尽管 octagon1
和 octagon2
是 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.