PHP 实现:
/ * Class Employee * 雇员类 */ class Employee { private $_name; private $_dept;//部门 private $_salary;//薪水 private $_subordinates;//下属 public function __construct(string $name, string $dept, int $sal) { $this->_name = $name; $this->_dept = $dept; $this->_salary = $sal; $this->_subordinates = []; } public function add(Employee $employee) { array_push($this->_subordinates, $employee); } public function remove(Employee $employee) { $this->_subordinates = array_filter($this->_subordinates, function ($v) use ($employee) { return ($employee != $v); }); } public function getSubordinates() { return $this->_subordinates; } public function __toString() { // TODO: Implement __toString() method. return "Employee: [name: " . $this->_name . ",dept: " . $this->_dept . ",salary: " . $this->_salary . "]"; } } class Demo { public static function main() { $CEO = new Employee("John", "CEO", 30000); $headSales = new Employee("Robert", "Head Sales", 20000); $headMarketing = new Employee("Michel", "Head Marketing", 20000); $clerk1 = new Employee("Laura", "Marketing", 10000); $clerk2 = new Employee("Bob", "Marketing", 10000); $salesExecutive1 = new Employee("Richard", "Sales", 10000); $salesExecutive2 = new Employee("Rob", "Sales", 10000); $CEO->add($headSales); $CEO->add($headMarketing); $headSales->add($salesExecutive1); $headSales->add($salesExecutive2); $headMarketing->add($clerk1); $headMarketing->add($clerk2); echo $CEO . PHP_EOL; $headMarketing->remove($clerk2); foreach ($CEO->getSubordinates() as $employee) { echo '-' . $employee . PHP_EOL; foreach ($employee->getSubordinates() as $e) { echo '---' . $e . PHP_EOL; } } } } Demo::main();
版权声明:
本文来源网络,所有图片文章版权属于原作者,如有侵权,联系删除。
本文网址:https://www.bianchenghao6.com/java-jiao-cheng/9670.html