我正在尝试测试封装。
我有两个对象部门和雇员。
部门通过Employee的实例,然后按照以下规则测试封装性
1。显示员工详细信息
2。显示部门详细信息
3。更改Employee objec中的值
4。再次显示部门详细信息(信息不应更改)
5。再次显示员工详细信息(此信息应在此处更改。)>
这有效,但是通过创建一个employee1的新实例,我是否得到封装错误的想法?????
或
我应该为真正的封装设置值
employee1.setName("Sam")
这将部门名称的第二个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()); } }
我正在尝试测试封装。我有两个对象部门和雇员。 Department传递了Employee的一个实例,然后按照以下规则测试封装性:1.显示...
没有“测试”封装之类的东西。
您无需编写任何代码来确定您的类是否正确遵循了封装原理。封装是面向对象的分析和设计指南。不是编程功能。