/*定义员工类*/
class Employee
{
var $id;
var $name;
var $birthday;
var $salary;
//构造函数 用于对象的实例化 没有返回值
function Employee($id,$name,$birthday,$salary){
$this->setId($id);
$this->setName($name);
$this->setBirthday($birthday);
$this->setSalary($salary);
}
function setId($id){
$this->id=$id;
}
function setName($name){
$this->name=strtoupper($name);
}
function setBirthday($birthday){
$this->birthday=$birthday;
}
function setSalary($salary){
$this->salary=$salary;
}
function getId(){
return $this->id;
}
function getName(){
return $this->name;
}
function getBirthday(){
return $this->birthday;
}
function getSalary(){
return $this->salary;
}
function getEmployeeInfo(){
$info="员工号:".$this->getId()."|";
$info.="姓名:".$this->getName()."|";
$info.="生日:".$this->getBirthday()."|";
$info.="工资:".$this->getSalary()."<br>";
return $info;
}
}
//利用构造函数进行实例化
$employee=new Employee(2007125,"employee1","1981-1-24",2000);
//$employee=new Employee
//$employee->setId(2007125);
//$employee->setName("wangjiafeng");
//$employee->setBirthday("1981-1-24");
//$employee->setSalary("2000");
print_r($employee);
echo "/n";
echo $employee->getEmployeeInfo();
echo "/n";
/*定义项目经理类,继承Employee类*/
class ProjectManager extends Employee{
var $project_name; //负责项目名称
var $sub_employes=array(); //职员的名单
//构造函数
function ProjectManager($id,$name,$birthday,$salary){
parent::Employee($id, $name, $birthday, $salary); //parent:: $this-> 这里一个效果
}
function setProjectName($pjname){
$this->project_name=$pjname;
}
function getProjectName(){
return $this->project_name;
}
function addEmployee(&$employee){
$this->sub_employes[]=& $employee;
}
//重写父类的方法
function getEmployeeInfo(){
$info="项目名:<b>".$this->getProjectName()."</b><br>"; //方法没有参数必须加()
$info.="员工号:".$this->getId()."|";
$info.="姓名:".$this->getName()."|";
$info.="生日:".$this->getBirthday()."|";
$info.="工资:".$this->getSalary()."<br>";
$info.="-------------------------------------------------------<br>";
foreach($this->sub_employes as $user)
{
$info.=$user->getEmployeeInfo();
}
return $info;
}
}
$manager=new ProjectManager(2007120,"manager","1981-1-24",8000);
$manager->setProjectName("网上商城购物系统-PHP");
$manager->addEmployee($employee); //按值传递如果外面$employee发生改变,那么$manager的内部成员变量$sub_employes也发生变化
$employee->setSalary(3000);
print_r($manager);
$employee1=new Employee(2007126,"employee2","1981-3-24",2020);
$manager->addEmployee($employee1);
echo "/n";
echo $manager->getEmployeeInfo();
echo "/n";