欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

java遞歸公司組織和員工

陳思宇1年前7瀏覽0評論

Java的遞歸函數可以應用于許多場景,包括公司組織和員工的管理。下面是一個簡單的例子:

public class Employee {
private String name;
private Listsubordinates;
public Employee(String name) {
this.name = name;
subordinates = new ArrayList<>();
}
public void addSubordinate(Employee employee) {
subordinates.add(employee);
}
public ListgetSubordinates() {
return subordinates;
}
public String toString() {
return "Employee{" +
"name='" + name + '\'' +
", subordinates=" + subordinates +
'}';
}
}
public class Company {
private Employee ceo;
public Company(String ceoName) {
ceo = new Employee(ceoName);
}
public void addEmployee(Employee employee, Employee manager) {
manager.addSubordinate(employee);
}
public void printOrganization() {
printEmployee(ceo, 0);
}
private void printEmployee(Employee employee, int level) {
for (int i = 0; i< level; i++) {
System.out.print("-");
}
System.out.println(employee.getName());
for (Employee subordinate: employee.getSubordinates()) {
printEmployee(subordinate, level + 1);
}
}
}
public class Main {
public static void main(String[] args) {
Company company = new Company("Tom");
Employee employee1 = new Employee("Alice");
Employee employee2 = new Employee("Bob");
Employee employee3 = new Employee("Charlie");
Employee employee4 = new Employee("David");
Employee employee5 = new Employee("Eve");
Employee employee6 = new Employee("Frank");
company.addEmployee(employee1, company.getCeo());
company.addEmployee(employee2, employee1);
company.addEmployee(employee3, employee1);
company.addEmployee(employee4, employee2);
company.addEmployee(employee5, employee3);
company.addEmployee(employee6, employee5);
company.printOrganization();
}
}

在這個例子中,我們創建了一個名為Employee的類,用于表示公司的員工。每個員工有自己的名字,以及可能的下屬。然后,我們創建了一個名為Company的類,用于存儲整個公司的組織結構。公司由一個最高領導者CEO開始,然后逐漸根據員工的關系建立子節點。

為了輸出整個組織結構,我們可以調用printOrganization()函數。這個函數使用了一個遞歸算法,在遍歷每個員工之前打印出當前層次的縮進。這樣,我們可以清晰地看到每個員工和它的下屬。