java 递归查询上级部门
时间: 2023-12-18 22:30:10 浏览: 288
```java
public class Department {
private String name;
private Department parent;
public Department(String name, Department parent) {
this.name = name;
this.parent = parent;
}
public String getName() {
return name;
}
public Department getParent() {
return parent;
}
public String findTopLevelDepartment() {
if (parent == null) {
return name;
} else {
return parent.findTopLevelDepartment();
}
}
}
// 示例
Department sales = new Department("Sales", null);
Department regionalSales = new Department("Regional Sales", sales);
Department localSales = new Department("Local Sales", regionalSales);
System.out.println(localSales.findTopLevelDepartment()); // 输出:Sales
```
阅读全文