复制构造函数测试不起作用(Java)

时间:2015-09-07 19:20:09

标签: java object copy

我正在从事CS家庭作业并遇到了问题。作业的结尾询问使用复制构造函数。目标是"制作一个Payroll对象,实例化它,制作第二个,然后打印它们。然后,更改第二个Payroll对象中的值,并显示更改的值仅显示在一个而不是两个中(即打印出原始值和稍微更改值的副本)。"我尝试更改第二个Payroll对象中的值,但它也在第一个更改它。我在下面列出了我的代码:

import java.util.Random;


public class Payroll {
private int[] employeeId;
private int[] hours;
private double[] payRate;

public Payroll(){
    this.employeeId = new int[0];
    this.hours = new int[0];
    this.payRate = new double[0];
}

public Payroll(Payroll obj){
    this.employeeId = obj.employeeId;
    this.hours = obj.hours;
    this.payRate = obj.payRate;
}

public Payroll(int i){
    this.employeeId = new int[i];
    this.hours = new int[i];
    this.payRate = new double[i];

}

public int getEmployeeIdAt(int index){
    return employeeId[index];   
}

public int getHoursAt(int index){
    return hours[index];
}

public double getPayRateAt(int index){
    return payRate[index];
}

public double getGrossPay(int index){
    double grossPay = hours[index] * payRate[index];
    grossPay = Math.round(grossPay * 100);
    return grossPay/100;
}

public void setEmployeeIdAt(int index, int id){
    this.employeeId[index] = id;
}

public void setHoursAt(int index, int hrs){
    this.hours[index] = hrs;
}

public void setPayRateAt(int index, double pr){
    this.payRate[index] = pr;
}

public void setHoursAt(int i){
    Random rand = new Random();
    int randHours = rand.nextInt((50 - 15) + 1) + 15;
    this.hours[i] = randHours;
}
}

import java.util.Scanner;


public class PayrollDriver {

public static void main(String[] args) {
    Payroll pr = new Payroll(5);
    Scanner scan = new Scanner(System.in);
    int empID = 1001;
    for(int i = 0; i < 5; i++){
        pr.setEmployeeIdAt(i, empID);
        empID++;
    }
    for(int i = 0; i < 5; i++){
        System.out.println("Enter the hourly pay rate for employee number "        + pr.getEmployeeIdAt(i) + ": ");
        double payRate = scan.nextDouble();
        if(payRate < 7.50){
            do{
                System.out.println("ERROR: Enter 7.50 or greater for pay rate: ");
                payRate = scan.nextDouble();
            } while(payRate < 7.50);
        }
        pr.setPayRateAt(i, payRate);
        pr.setHoursAt(i);
    }
    System.out.println("PAYROLL DATA");
    System.out.println("======================");
    for(int i = 0; i < 5; i++){
        System.out.println("Employee ID: " + pr.getEmployeeIdAt(i) + "       Hours: " + pr.getHoursAt(i) + "    Rate: " + pr.getPayRateAt(i) + 
                "    Gross Pay: $" + pr.getGrossPay(i));
    }
    System.out.println("Would you like to run the Copy Constructor Test? Enter 'y' (lowercase) if yes, enter any other letter if no: ");
    char copyTestVerify = scan.next().charAt(0);
    if(copyTestVerify == 'y'){
        CopyConstructorTest ct = new CopyConstructorTest();
        ct.CopyTest();
    }

    scan.close();
}

}

以下是我的CopyConstructorTest类,它测试复制构造函数是否会更改原始对象的值:

public class CopyConstructorTest {
    public void CopyTest(){
        Payroll pay = new Payroll(5);
        pay.setEmployeeIdAt(0, 1001);

        Payroll payCopy = new Payroll(pay);
        System.out.println("Original: " + pay.getEmployeeIdAt(0));
        System.out.println("Copy: " + payCopy.getEmployeeIdAt(0));
        payCopy.setEmployeeIdAt(0, 5000);
        System.out.println("Original after changes: " +     pay.getEmployeeIdAt(0));
        System.out.println("Copy after changes: " + payCopy.getEmployeeIdAt(0));

    }
}

我对自己做错了什么并不积极。非常感谢任何帮助或指导。

1 个答案:

答案 0 :(得分:1)

您只是复制对数组的引用,而不是实际数据。因此,每当您更改其中一个对象中的数据时,两者都会看到更改,因为它们指向同一个数组 复制数据的最简单方法可能是使用System.arraycopy()

public Payroll(Payroll obj) {
    this.employeeId = new int[obj.employeeId.length];
    System.arraycopy(obj.employeeId, 0, this.employeeId, 0, obj.employeeId.length);
    ...