abstract class EmployeeType{
private static $_a = 1;
private static $_b = 2;
private static $_c = 3;
private static $_d = 4;
public static function getType($type = null){
switch ($type){
case self::$_a:
return new A();
break;
case self::$_b:
return new B();
break;
case self::$_c:
return new C();
break;
case self::$_d:
return new D();
break;
default :
return null;
}
}
abstract function payAcount();
}
class A extends EmployeeType{
public function payAcount(){
return 111;
}
}
class B extends EmployeeType{
public function payAcount() {
return 222;
}
}
class C extends EmployeeType{
public function payAcount(){
return 333;
}
}
class D extends EmployeeType{
public function payAcount(){
return 'ddddd';
}
}
class Employee{
private $_type;
function __construct($type){
$this->_type = EmployeeType::getType($type);
}
public function setType($type){
$this->_type = EmployeeType::getType($type);
}
public function payAcount(){
return empty($this->_type) ? 'this not a object' : $this->_type->payAcount();
}
}
$employeeObj = new Employee(33);
echo $employeeObj->payAcount();
php 多太性
最新推荐文章于 2024-04-13 22:32:03 发布
本文探讨了使用抽象类和具体子类实现不同员工类型的模式,通过EmployeeType类定义了员工类型并使用switch-case结构来实例化不同类型的员工对象,展示了如何通过父类接口和子类实现来灵活管理不同类型员工的计费逻辑。
摘要由CSDN通过智能技术生成