为什么我们需要复制构造函数以及何时应该在java中使用复制构造函数 [英] Why do we need copy constructor and when should we use copy constructor in java

查看:148
本文介绍了为什么我们需要复制构造函数以及何时应该在java中使用复制构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览Copy Constructors,我已经浏览了堆栈中的链接和其他链接。但我不清楚以下几点。

I was going through Copy Constructors, I have gone through the links in stack over flow and others as well. But i am not clear on the following points.


  1. 为什么我们需要复制构造函数

  2. 我们什么时候需要复制构造函数

我的意思是我们需要使用Copy Constructor的确切情况或场景是什么。有人可以用一个例子解释或指出链接,这样我就可以明确地理解它们。

I mean what is the exact situation or scenario we would need to use Copy Constructor. Can some one explain with an example or point out links so that i can go through and understand them in clear.

以下是我为了解而理解的链接什么是复制构造函数。

Following are the links i have gone through to get an understanding of what is a copy constructor.

http://www.programmerinterview.com/index.php/java-questions/how-copy-constructors-work/

https://deepeshdarshan.wordpress.com/2013/12/05/copy-constructors-in-java/

第二个链接解释了为什么和在哪里使用复制构造函数。但我仍然不清楚。

The second link explains 'why' and 'where' the copy constructor is to be used. But still i am not clear on it.

以下是我的班级Employee.java

Below is my class Employee.java

package com.test;

/**
 * @author avinashd
 *
 */
public class Employee {

    private String rollNo;
    private String name;

    //constructor
    public Employee(String rollNo, String name){

        this.rollNo = rollNo;
        this.name = name;
    }

    //copy constructor
    public Employee(Employee employee){

    this.rollNo = employee.rollNo;
    this.name = employee.name;

    }

    public String getRollNo() {
        return rollNo;
    }

    public void setRollNo(String rollNo) {
        this.rollNo = rollNo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

复制构造函数用于创建和精确复制具有相同现有对象值的对象。

Copy Constructor is used to create and exact copy of an object with the same values of an existing object.

例如,我们有一个员工,其值为 rollNo:1 name:avinash 。复制构造函数将创建一个类似的对象,其值为 rollNo:1 name:avinash 。但两者都是2个不同的对象,对on对象值的更改不会影响另一个对象。

Say for example we have an Employee with values as rollNo: 1 and name: avinash. Copy Constructor would create a similar object with values as rollNo: 1 and name: avinash . But both are 2 different objects and changes to the values of on object will not affect another object.

这里的问题是

当我们有一个构造函数,例如

When we have a constructor such as

public Employee(String rollNo, String name){
    this.rollNo = rollNo;
    this.name = name;
}

创建一个对象。我们可以调用相同的构造函数来创建另一个对象。但是为什么我们需要调用copy构造函数。什么时候需要调用它?请解释

to create an object. We can call the same constructor to create another object. But why do we need to call copy constructor.When do we need to call it ?. Please explain

推荐答案

使用复制构造函数而不是传递所有参数的构造函数有两个很好的理由:

There are 2 good reasons for using a copy constructor instead of the constructor passing all parameters :


  1. 当你有一个具有许多属性的复杂对象时,使用复制构造函数要简单得多

  2. 如果添加一个属性到您的类,您只需更改复制构造函数以考虑此新属性,而不是更改其他构造函数的每个出现

这篇关于为什么我们需要复制构造函数以及何时应该在java中使用复制构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆