任务:
定义如下类
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
要求: 在⼀个对象创建和消亡时在控制台输出相应消息 吃饭后体重会增加 睡觉后⾝⾼会增加 跑步、游泳会降低体重 过⽣⽇会长⼀岁 每个⽅法被执⾏的时候都需要调⽤speak⽅法,输出当前调⽤的⽅法的名字 每当⼀个新的对象被创建,currentNumber需要发⽣变化,当总⼈数超过MAXNUMBER时提⽰创建失败。
反思
之前的任务完成时构造函数中出现了return,并且调用析构函数并不能完全销毁对象,所以进行改进,增加了set.php进行对象创建控制,People.php中定义了类People,set.php中定义了set函数用于创建对象,main.php实现具体功能.
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();
}
?>
网友评论