任務(wù):
定義如下類
People
屬性: name (string nonstatic) age (int nonstatic) height (double nonstatic) weight (duoble nonstatic) hobby (array nonstatic) currentNumer (int static) MAXNUMBER (const)
?法 speak eat haveBirthday sleep walk swim run
要求: 在?個對象創(chuàng)建和消亡時在控制臺輸出相應(yīng)消息 吃飯后體重會增加 睡覺后??會增加 跑步窗悯、游泳會降低體重 過??會長?歲 每個?法被執(zhí)?的時候都需要調(diào)?speak?法单料,輸出當(dāng)前調(diào)?的?法的名字 每當(dāng)?個新的對象被創(chuàng)建掰担,currentNumber需要發(fā)?變化来农,當(dāng)總?數(shù)超過MAXNUMBER時提?創(chuàng)建失敗。
反思
之前的任務(wù)完成時構(gòu)造函數(shù)中出現(xiàn)了return蒿偎,并且調(diào)用析構(gòu)函數(shù)并不能完全銷毀對象性雄,所以進行改進,增加了set.php進行對象創(chuàng)建控制戳晌,People.php中定義了類People,set.php中定義了set函數(shù)用于創(chuàng)建對象痴柔,main.php實現(xiàn)具體功能.
People.php
<?php
namespace MyProject;
class People
{
private $name;
private $age;
private $height;
private $weight;
private $hobby;
public static $currentNumber = 0;
const MAXNUMBER = 2;
/**
* People constructor.
* @param string $name
* @param int $age
*/
public function __construct(string $name, int $age, $height, $weight, array$hobby)
{
$this->name = $name;
$this->age = $age;
$this->height = $height;
$this->weight= $weight;
$this->hobby= $hobby;
self::$currentNumber++ ;
echo "current people number " .self::$currentNumber."</br>\n";
echo "the person's name is $this->name,</br>
the age is $this->age years old,</br>
the height is $this->height,</br>
the weight is $this->weight,</br>
the hobby is </br>\n";
var_dump($hobby);
echo "</br>\n";
}
public function speak($action){
echo "the method is $action ";
echo "      ";
}
public function eat(){
$this->speak(__METHOD__ );
$this->weight=$this->weight+1;
echo "current weight is ".$this->weight."</br>\n";
}
public function sleep(){
$this->speak(__METHOD__ );
$this->height=$this->height+1;
echo "current height is ".$this->height."</br>\n";
}
public function run(){
$this->speak(__METHOD__ );
$this->weight=$this->weight-1;
echo "current weight is ".$this->weight."</br>\n";
}
public function swim(){
$this->speak(__METHOD__ );
$this->weight=$this->weight-1;
echo "current weight is ".$this->weight."</br>\n";
}
public function haveBirthday(){
$this->speak(__METHOD__ );
$this->age=$this->age+1;
echo "current age is ".$this->age."</br>\n";
}
public function __destruct(){
echo "destruct of " . __CLASS__ . " " . __METHOD__ . "</br>\n";
}
}?>
set.php
<?php
namespace task2\file1;
require "People.php";
//use MyProject;
use MyProject\People ;
function set(string $name, int $age, $height, $weight, array $hobby)
{
if (People::$currentNumber+1> People::MAXNUMBER) {
echo "construct fails ";
echo "            ";
echo "</br>\n";
echo "</br>\n";
return null;
}
else
{
$person =new People($name, $age, $height, $weight, $hobby);
return $person;
}
}
?>
main.php
<?php
require "set.php";
use function task2\file1\set as set;
$hooby = array(
0=>"play basketball",
1 => "play football",);
$people = set("ieso1",22,170,110,$hooby);
if(!empty($people)) {
$people->haveBirthday();
$people->eat();
$people->sleep();
$people->swim();
$people->run();}
echo "</br>\n";
$people1 = set("ieso2",23,180,120,$hooby);
echo "</br>\n";
$people2 = set("ieso3",24,185,130,$hooby);
if(!empty($people2))
{
$people2->eat();
}
$people3 = set("ieso3",24,185,130,$hooby);
if(!empty($people3))
{
$people3->eat();
}
?>
運行結(jié)果
task2運行結(jié)果.jpg