Having a class itself as its constructor's only parameter

前端 未结 4 1953
星月不相逢
星月不相逢 2021-01-20 09:51
public class Foo{
    private String a;
    private int b;

    public Foo(Foo foo){
        this.a = foo.a;
        this.b = foo.b;
    }
}

Hi eve

4条回答
  •  爱一瞬间的悲伤
    2021-01-20 10:16

    There is nothing wrong with your code. Actually what you did has a name: copy constructor And it is very convenient way to make o copy of another object. (assuming you have some other way to create instance of it besides this constructor )

提交回复
热议问题