根据你应该有的问题 collection 的 Employee 宾语。所以你必须创造 Employee Collection 这样 List , Set 然而,你不能限制容量a Collection 这两个值之间。以编程方式,你唯一能做的就是 throw 一个 Exception 当调用getter方法时 Collection 。你可以把两个默认值 Collection 。我不相信那会对你有用。在您的代码中进行以下修改。
collection
Employee
Collection
List
Set
throw
Exception
public class Department { private String departmentName; private String locationofDep; //collection of empleyees Set<Employee> employees = new HashSet<>(); //use this method to add employees public void addEmployee(Employee employee) { this.employees.add(employee);; } public Set<Employee> getEmployees() throws Exception { if (this.employees.size() < 2 || this.employees.size() > 10) { throw new Exception("Employees out of capacity"); } return this.employees; } //other getters and setters should be here } public class Employee { private String empName; private String dob; //put getters and setters }
因此,您可以在使用员工信息时管理员工长度问题 try-catch 块
try-catch