我正在尝试测试封装,如果我调用新的 Obj 进行测试,它是否有效?
I am trying to test for encapsulation, does it count if I call new Obj to test?
我正在尝试测试封装。
我做了两个对象Department和Employee。
部门获得一个 Employee 实例,然后测试封装我遵循这些规则
1.Display 员工详细信息
2.Display部门详情
3.Change Employee 对象中的值
4.Display 部门详细信息(信息不应更改)
5.Again显示员工详细信息(此处应更改信息)。
这行得通,但我是不是通过创建 employee1 的新实例而错误地理解了封装的想法????
或
我是否应该设置真正封装的值
employee1.setName("Sam")
这会将 Department name 的第二次 display() 调用更改为 Sam。
//Main
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 );
Department mainDepartment = new Department("Main Floor", employee1);
//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();
}
}
//Employee Class
package question1;
public class Employee {
private String ppsNum;
private String name;
private double salary;
//Parameterized constructor
public Employee(String ppsNum, String name, double salary) {
this.ppsNum = ppsNum;
this.name = name;
this.salary = 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");
}}
//Department Class
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());
}
}
不存在"testing"封装
您无需编写任何代码来确定您的 class 是否正确遵循了封装原则。封装是一种面向对象的分析和设计指南。不是编程功能。
良好的封装意味着您遵循两个步骤:
- 所有相关信息应该放在一起。 例如:Employee 应该只有Employee 信息,Department 应该只有Department 信息。员工不应存储特定部门所在的楼层。或者 Even 不应该有一个名为 seperationLine() 的方法。 (IMO,seperationLine() 方法属于另一个 Presentor class)
- 只应提供必需的信息public。其余所有信息都应保密或受保护。目标不是保密,而是防止外部参与者修改他们不应该修改的信息而引起的潜在问题。例如:员工不应设置部门楼层。
只需查看 Employee class 并将您认为不应从外部访问的所有字段和方法设置为私有。此外,对于Department 需要Employee 的信息,在Employee class 中创建一个Department 可以调用的方法。这样,部门不能修改 Employee,但它可以访问它需要的信息。
我正在尝试测试封装。
我做了两个对象Department和Employee。
部门获得一个 Employee 实例,然后测试封装我遵循这些规则
1.Display 员工详细信息
2.Display部门详情
3.Change Employee 对象中的值
4.Display 部门详细信息(信息不应更改)
5.Again显示员工详细信息(此处应更改信息)。
这行得通,但我是不是通过创建 employee1 的新实例而错误地理解了封装的想法????
或
我是否应该设置真正封装的值
employee1.setName("Sam")
这会将 Department name 的第二次 display() 调用更改为 Sam。
//Main
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 );
Department mainDepartment = new Department("Main Floor", employee1);
//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();
}
}
//Employee Class
package question1;
public class Employee {
private String ppsNum;
private String name;
private double salary;
//Parameterized constructor
public Employee(String ppsNum, String name, double salary) {
this.ppsNum = ppsNum;
this.name = name;
this.salary = 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");
}}
//Department Class
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());
}
}
不存在"testing"封装
您无需编写任何代码来确定您的 class 是否正确遵循了封装原则。封装是一种面向对象的分析和设计指南。不是编程功能。
良好的封装意味着您遵循两个步骤:
- 所有相关信息应该放在一起。 例如:Employee 应该只有Employee 信息,Department 应该只有Department 信息。员工不应存储特定部门所在的楼层。或者 Even 不应该有一个名为 seperationLine() 的方法。 (IMO,seperationLine() 方法属于另一个 Presentor class)
- 只应提供必需的信息public。其余所有信息都应保密或受保护。目标不是保密,而是防止外部参与者修改他们不应该修改的信息而引起的潜在问题。例如:员工不应设置部门楼层。
只需查看 Employee class 并将您认为不应从外部访问的所有字段和方法设置为私有。此外,对于Department 需要Employee 的信息,在Employee class 中创建一个Department 可以调用的方法。这样,部门不能修改 Employee,但它可以访问它需要的信息。