当前位置:网站首页 > Java教程 > 正文

java组合模式教程



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();

  • 上一篇: java教程220
  • 下一篇: 盾构机java教程
  • 版权声明


    相关文章:

  • java教程2202025-01-27 09:50:03
  • java 推箱子 教程2025-01-27 09:50:03
  • java教程443集2025-01-27 09:50:03
  • 教程java在线2025-01-27 09:50:03
  • 迷你世界java教程2025-01-27 09:50:03
  • 盾构机java教程2025-01-27 09:50:03
  • java模组导入教程2025-01-27 09:50:03
  • java reflections 教程2025-01-27 09:50:03
  • java教程421集2025-01-27 09:50:03
  • java傻瓜安装教程2025-01-27 09:50:03