因此,我已经完成了下面代码中任务所需要的一切。

我遇到的问题是关于


对象的防御副本
更改Employee对象中的值。


对于2

我更改了值,但我称之为new,它正在创建对象的新实例。

employee1 = new Employee("626347B", "Sam O'Conor", 24000);

这样,问题在我第二次调用mainDepartment.display();时起作用,值相同。

但这在我创建新对象时感觉不像是正确的封装。

我在想

employee1.setName("Conor Bryan);

这是测试封装性的方法,现在当我调用mainDepartment.display();时,名称值确实发生了变化,因此问题是错误的。

! - - -题 - - -!

显示员工详细信息

显示部门详细信息

更改Employee对象中的值。

再次显示部门详细信息(信息不应更改)

再次显示员工详细信息(信息应在此处更改)。

! - - 测试 - - !

package question1;

public class Test {

    public static void main(String[] args) {
        //Creating a instance of both Employee and Department
        Employee employee1 = new Employee("2726354E", "Bob Ings", 30000 );

        //Updated Code for Department to take a copy of Employee
        Employee copy = new Employee("2726354E", "Bob Ings", 30000 );

        Department mainDepartment = new Department("Main Floor", copy);

        //Displaying both instances of Employee and Department
        employee1.display();
        mainDepartment.display();

        System.out.println("");


        //Changing values in constructor for the instance of Employee we made earlier on
        employee1 = new Employee("626347B", "Sam O'Conor", 24000);

        mainDepartment.display();

        System.out.println("");
        System.out.println("");

        employee1.display();

    }

}


! - - 部门 - - !

package question1;

public class Department {
    private String deptName;
    private Employee employee;
    private int officeNumber;

    //Constructor with all three parameters
    public Department(String deptName, Employee employee, int officeNumber) {

        this.deptName = deptName;
        this.employee = employee;
        this.officeNumber = officeNumber;
    }

    //Constructor with the officeNumber set to 0
    public Department(String deptName, Employee employee) {

        this.deptName = deptName;
        this.employee = employee;
        this.officeNumber = 0;
    }

    //Displaying the instance of the object information in a anesthetically pleasing manner
    public void display() {
        System.out.println("Department");
        Employee.seperationLine();
        System.out.println("Department Name: " + getDeptName());
        Employee.seperationLine();
        System.out.println("Employee: " + employee.toString());
        Employee.seperationLine();
        System.out.println("Office Number: " + getOfficeNumber());

    }



    //Getters and Setters
    public String getDeptName() {
        return deptName;
    }

    public void setDeptName(String deptName) {
        this.deptName = deptName;
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }

    public int getOfficeNumber() {
        return officeNumber;
    }

    public void setOfficeNumber(int officeNumber) {
        this.officeNumber = officeNumber;
    }


}


! - - 雇员 - - !

 package question1;

public class Employee {
    private String ppsNum;
    private String name;
    private double salary;

    public Employee() {}

    //Parameterized constructor
    public Employee(String ppsNum, String name, double salary) {
        this.ppsNum = ppsNum;
        this.name = name;
        this.salary = salary;
    }

    //Defensive copy constructor
    public Employee(Employee copy) {
        this.ppsNum = copy.ppsNum;
        this.name = copy.name;
        this.salary = copy.salary;
    }

    //Displaying the instance of the object information in a anesthetically pleasing manner
    public void display() {
        System.out.println("Employee Information");
        seperationLine();
        System.out.println("Name: " + getName());
        seperationLine();
        System.out.println("PPS number: " + getPpsNum());
        seperationLine();
        System.out.println("Salary: " + getSalary() + "0");
        seperationLine();
        System.out.println("\n");


    }

    public String toString() {
        return "[ppsNum=" + ppsNum + ", name=" + name + ", salary=" + salary + "]";
    }

    //Getters and Setters
    public String getPpsNum() {
        return ppsNum;
    }

    public void setPpsNum(String ppsNum) {
        this.ppsNum = ppsNum;
    }

    public String getName() {
        return name;
    }

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

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public static void seperationLine() {
        System.out.println("------------------------");
    }






}

最佳答案

employee1 = new Employee("626347B", "Sam O'Conor", 24000);


没有更改Employee对象中的值,而是创建了一个新的Employee对象。


  我在想

employee1.setName("Conor Bryan);



是的,这是更改对象中值的正确方法。这样做而不是创建一个新对象,然后显示结果,您应该看到employee1的值已更改。

为了满足第二个要求(部门不变),您需要在将雇员传递给部门时制作一份雇员副本。

//Constructor with all three parameters
public Department(String deptName, Employee employee, int officeNumber) {

    this.deptName = deptName;
    this.employee = employee;  // make a copy here
    this.officeNumber = officeNumber;
}


最简单的方法可能是在Employee类中实现copy constructor,然后在Department中使用它。您需要在Department中设置employee属性的任何位置进行复制,因此无论是在构造函数中还是在setEmployee方法中。

10-04 14:01
查看更多