homework3

name = $vName;
        $this->weight = $vWeight;
        $this->height = $vHeight;
        $this->age = $vAge;
        $this->hobby = $aHobby;
        echo "Here comes a newbee! Mr $vName! 
"; } //析构函数 public function __destruct(){ echo "Mr ".$this->name." left.
"; self::$crtNum--; } //数据成员 public $name = ""; public $weight = 70; public $height = 180; public $age = 20; public $hobby = array(); } class PersonShell{ //构造函数 public function __construct($vName,$vAge,$vWeight,$vHeight,array $aHobby){ if(++Person::$crtNum > Person::maxNum) { echo "Too many people!
"; } else { $this->person = new Person($vName,$vAge,$vWeight,$vHeight,$aHobby); } } //接口函数 public function eat(){ $this->speak(__FUNCTION__); $this->person->weight++; } public function sleep(){ $this->speak(__FUNCTION__); $this->person->height++; } public function swim(){ $this->speak(__FUNCTION__); if(rand(0,9) == 5){ echo "Ow! Mr ".$this->person->name." got drown
"; //销毁实例 unset($this->person); } else { $this->person->weight--; } } public function birthday(){ $this->speak(__FUNCTION__); $this->person->age++; } //数据成员 private $person; //工具函数 private function speak($crtAct){ echo "Mr ".$this->person->name." is going to ".$crtAct.".
"; } } $person1 = new PersonShell("John",45,80,185,array("1","2","3")); $John->eat(); $John->swim(); $person2 = new PersonShell("Tom",20,80,185,array("1","2","3")); $person3 = new PersonShell("Mia",30,80,185,array("1","2","3")); $person4 = new PersonShell("Leo",10,80,185,array("1","2","3")); ?>
homework3_第1张图片
图片发自App

homework3_第2张图片
图片发自App
homework3_第3张图片
图片发自App
homework3_第4张图片
图片发自App

你可能感兴趣的:(homework3)