Java clone

来源:互联网 发布:工业集成网络 编辑:程序博客网 时间:2024/06/05 17:09

 

package Clone;

public class Point extends Object implements Cloneable {

    public int x;
    public int y;
   
    public Object clone() throws CloneNotSupportedException{
 Object o;
 o = super.clone();
 
 return o;
    }
}

 

package Clone;

public class CloneTest {

    /**
     * @param args
     * @throws CloneNotSupportedException
     */
    public static void main(String[] args) throws CloneNotSupportedException {
 Point a = new Point();
 a.x = 11;
 a.y = 12;
 
 Point b = a;
 b= (Point) a.clone();
 b.x = 1;
 b.y = 2;

 System.out.println(a.x + a.y);
 System.out.println(b.x + b.y);
    }

   
}

23
3

原创粉丝点击