Can clone method create object using constructor

前端 未结 2 1969
别跟我提以往
别跟我提以往 2021-01-02 10:49

I always thought that clone() creates an object without calling a constructor.

But, while reading Effective Java Item 11: Override clone judicio

2条回答
  •  隐瞒了意图╮
    2021-01-02 11:41

    I think below part of code always create the new object and supply to the other class (i.e. helpfull in singleton).

     @Override
        public Object clone() {
            return new Foo(bar);
        }
    

    I think this can be supply for the purpose:

    public MyObject clone() {
        return new MyObject(this);
    }
    

提交回复
热议问题