小编lfe*_*z93的帖子

克隆如何比对象创建具有更多性能

我试图理解java中clone()方法下面发生了什么,我想知道如何做一个新的调用

public class Person implements Cloneable {

    private String firstName;
    private int id;
    private String lastName;

    //constructors, getters and setters

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Person p = (Person) super.clone();
        return p;
    }

}
Run Code Online (Sandbox Code Playgroud)

这是我的克隆代码我想知道底下发生了什么,以及新呼叫之间有什么区别因为.

这是我的客户端代码

    Person p = new Person("John", 1, "Doe");
    Person p2 = null;
    try {
         p2 = (Person) p.clone();
    } catch (CloneNotSupportedException ex) {
        Logger.getLogger(clientPrototype.class.getName()).log(Level.SEVERE, null, ex);
    }
    p2.setFirstName("Jesus");
    System.out.println(p);
    System.out.println(p2);
Run Code Online (Sandbox Code Playgroud)

java performance clone cloneable

8
推荐指数
3
解决办法
6969
查看次数

标签 统计

clone ×1

cloneable ×1

java ×1

performance ×1