为动物熊创建一个PHP类
我正在寻找一种名为“Bear”的单一类能够:为动物熊创建一个PHP类,php,Php,我正在寻找一种名为“Bear”的单一类能够: 每两小时吃一次蜂蜜,记住他们上次吃蜂蜜的时间 决定他们是否需要睡觉 我想知道是否有一种方法可以做到这一点,而不必硬编码数组时间,也不必使用else语句来决定bear是否需要睡眠单独的函数会更好、更干净吗? class Bear { protected $name; protected $currentTime; public function setName($name) { $this->name = $name; } p
- 每两小时吃一次蜂蜜,记住他们上次吃蜂蜜的时间
- 决定他们是否需要睡觉
class Bear
{
protected $name;
protected $currentTime;
public function setName($name)
{
$this->name = $name;
}
public function getName()
{
return $this->name;
}
public function showLastFeedtime()
{
$lastFed = strtotime('-2 hour', strtotime($this->currentTime));
$newdate = date('H:i', $lastFed);
echo $this->name." last ate at ".$newdate."<br/><br/>";
}
public function checkFeedingTime($checkHours = array())
{
if(in_array($this->currentTime,$checkHours))
{
echo "It's ".$this->name."'s feeding time </br>";
echo "The time ".$this->currentTime. " is the time and I'm eating
honey at right now. </br></br>";
echo "These are the times I eat</br>";
foreach($checkHours as $feedingTimes)
{
echo $feedingTimes."</br>";
}
echo "<hr>";
}
else
{
//DECIDE IF THE BEAR NEEDS TO SLEEP
echo "Barney is having a snooze";
}
}
public function getTime($currentTime)
{
$this->currentTime = $currentTime;
}
public function setTime()
{
return $this->currentTime;
}
}
//new instance of barney
$barney = new Bear();
//set the bears name
$barney->setName('Barney');
//get the bears name
$bigBear = $barney->getName();
//page info
echo "<h1>".$bigBear."'s profile page </h1>";
echo "<hr>";
echo "I am a bear and my name is {$bigBear} <br/>";
echo "<hr>";
//the current time
$currentTime = date('H:i');
$barney->getTime($currentTime);
//last feeding time
$barney->showLastFeedtime();
//ARRAY TIMES-barney eats every 2 hours
$checkHours =
array('00:00','02:00','04:00','06:00','08:00',
'10:00','12:00','14:00','16:00','18:00','20:00','22:00');
//call the feeding time function
$barney->checkFeedingTime($checkHours);
?>
类熊
{
受保护的$名称;
受保护的$currentTime;
公共函数setName($name)
{
$this->name=$name;
}
公共函数getName()
{
返回$this->name;
}
公共函数showLastFeedtime()
{
$lastFed=strottime('-2小时',strottime($this->currentTime));
$newdate=日期($H:i',$lastFed);
echo$this->name.“上次吃东西是在”$newdate.“
”;
}
公共函数checkFeedingTime($checkHours=array())
{
if(在数组中($this->currentTime,$checkHours))
{
回显“这是“$this->name.”的喂食时间”;
回声“时间”。$this->currentTime。“是时间,我正在吃东西。”
现在就吃蜂蜜。“;
echo“这是我吃饭的时候”;
foreach($checkHours作为$feedingTimes)
{
echo$feedingTimes.“”;
}
回声“
”;
}
其他的
{
//决定熊是否需要睡觉
echo“Barney正在打盹”;
}
}
公共函数getTime($currentTime)
{
$this->currentTime=$currentTime;
}
公共函数设置时间()
{
返回$this->currentTime;
}
}
//巴尼的新实例
$barney=新熊();
//设定熊的名字
$barney->setName('barney');
//得到熊的名字
$bigBear=$barney->getName();
//页面信息
echo“$bigBear.”的个人资料页;
回声“
”;
echo“我是一只熊,我的名字是{$bigBear}
”;
回声“
”;
//当前时间
$currentTime=日期('H:i');
$barney->getTime($currentTime);
//上次喂食时间
$barney->showLastFeedtime();
//数组乘以barney每2小时吃一次
$checkHours=
数组('00:00'、'02:00'、'04:00'、'06:00'、'08:00',
'10:00','12:00','14:00','16:00','18:00','20:00','22:00');
//调用进给时间函数
$barney->checkFeedingTime($checkHours);
?>
一些短期课程:
<?php
// Ok, so we are in role of god and probably, we will want to create
// more animals than just bear, so we will prepare cheap interface for
// our creations
interface Animal {
// we should be aware, if our lil beast is hungry
public function isHungry(): bool;
// and we should know, that we can feed it
public function feed(): void;
public function live(): void;
public function isSleepy(): bool;
public function sleep(): void;
}
class State {
public $sleeping;
}
// Kay, now we can start with making our master beer
class Bear implements Animal {
/**
* He should have name
* @var string
*/
private $name;
/**
* We would like to know, when our bearsir was eating last time
* @var \DateTime
*/
private $lastEatingTime;
/**
* @var int
*/
private $energy;
/**
* @var State
*/
private $state;
// construction of bear, cause we can
public function __construct(string $name, \DateTime $lastEatingTime)
{
$this->name = $name;
$this->lastEatingTime = $lastEatingTime;
$this->energy = 100;
$state = new State();
$state->sleeping = false;
$this->state = $state;
}
// something like when you ask bear whats his name
public function getName(): string
{
return $this->name;
}
// something like you get new bear and you wanna rename him
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
// we have inteligent bear, so we can ask him about the last time of feeding
public function getLastEatingTime(): \DateTime
{
return $this->lastEatingTime;
}
// we can also force him to think he ate before one minute even if its not true
public function setLastEatingTime(\DateTime $lastEatingTime): self
{
$this->lastEatingTime = $lastEatingTime;
return $this;
}
// we can determine if he is hungry or not by comparing current time and last time he ate
public function isHungry(): bool
{
$now = new \DateTime();
return $now->diff($this->getLastEatingTime())->format('%h') >= 2;
}
// give him food
public function feed(): void
{
$this->energy += 5;
$this->setLastEatingTime(new \DateTime());
}
public function isSleepy(): bool
{
return $this->energy < 20;
}
/**
* we will give him some rest
*/
public function sleep(): void
{
$this->state->sleeping = true;
$this->energy += rand(5, 20)/10;
if ($this->energy >= 100) {
$this->wakeUp();
}
}
/**
* Hey, wake up bear!
*/
public function wakeUp(): void
{
$this->state->sleeping = false;
}
/**
* Makes one lifecycle of bear
*/
public function live(): void
{
$this->energy -= 1;
if ($this->state->sleeping) {
$this->sleep();
echo 'Im sleeping';
} else {
echo 'Im awake and ';
if ($this->isSleepy()) {
echo 'Im sleepy, so i go sleep';
$this->sleep();
} else {
if ($this->isHungry()) {
echo 'Im hungry, so im gonna eat some honey';
$this->feed();
} else {
echo 'im fine';
}
}
}
}
}
// lets make life
class Life {
public function live(Animal $animal)
{
// let life go on forever till some asteroid crashes our server
while (true) {
$animal->live();
sleep(1);
}
}
}
$myBear = new Bear("Stanley", new \DateTime());
$life = new Life();
$life->live($myBear);
一些短期课程:
<?php
// Ok, so we are in role of god and probably, we will want to create
// more animals than just bear, so we will prepare cheap interface for
// our creations
interface Animal {
// we should be aware, if our lil beast is hungry
public function isHungry(): bool;
// and we should know, that we can feed it
public function feed(): void;
public function live(): void;
public function isSleepy(): bool;
public function sleep(): void;
}
class State {
public $sleeping;
}
// Kay, now we can start with making our master beer
class Bear implements Animal {
/**
* He should have name
* @var string
*/
private $name;
/**
* We would like to know, when our bearsir was eating last time
* @var \DateTime
*/
private $lastEatingTime;
/**
* @var int
*/
private $energy;
/**
* @var State
*/
private $state;
// construction of bear, cause we can
public function __construct(string $name, \DateTime $lastEatingTime)
{
$this->name = $name;
$this->lastEatingTime = $lastEatingTime;
$this->energy = 100;
$state = new State();
$state->sleeping = false;
$this->state = $state;
}
// something like when you ask bear whats his name
public function getName(): string
{
return $this->name;
}
// something like you get new bear and you wanna rename him
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
// we have inteligent bear, so we can ask him about the last time of feeding
public function getLastEatingTime(): \DateTime
{
return $this->lastEatingTime;
}
// we can also force him to think he ate before one minute even if its not true
public function setLastEatingTime(\DateTime $lastEatingTime): self
{
$this->lastEatingTime = $lastEatingTime;
return $this;
}
// we can determine if he is hungry or not by comparing current time and last time he ate
public function isHungry(): bool
{
$now = new \DateTime();
return $now->diff($this->getLastEatingTime())->format('%h') >= 2;
}
// give him food
public function feed(): void
{
$this->energy += 5;
$this->setLastEatingTime(new \DateTime());
}
public function isSleepy(): bool
{
return $this->energy < 20;
}
/**
* we will give him some rest
*/
public function sleep(): void
{
$this->state->sleeping = true;
$this->energy += rand(5, 20)/10;
if ($this->energy >= 100) {
$this->wakeUp();
}
}
/**
* Hey, wake up bear!
*/
public function wakeUp(): void
{
$this->state->sleeping = false;
}
/**
* Makes one lifecycle of bear
*/
public function live(): void
{
$this->energy -= 1;
if ($this->state->sleeping) {
$this->sleep();
echo 'Im sleeping';
} else {
echo 'Im awake and ';
if ($this->isSleepy()) {
echo 'Im sleepy, so i go sleep';
$this->sleep();
} else {
if ($this->isHungry()) {
echo 'Im hungry, so im gonna eat some honey';
$this->feed();
} else {
echo 'im fine';
}
}
}
}
}
// lets make life
class Life {
public function live(Animal $animal)
{
// let life go on forever till some asteroid crashes our server
while (true) {
$animal->live();
sleep(1);
}
}
}
$myBear = new Bear("Stanley", new \DateTime());
$life = new Life();
$life->live($myBear);
你需要一种机制来跟上时间/状态。你需要一种机制来跟上时间/状态。你做了什么?它为什么有效?解释你的answer@Martin由于命名变量、类、类型和注释Serrr、no;没有“自我解释代码”这样的东西,否则它将不会被称为代码,您也不需要注释。评论非常有用;在你的答案中用评论和散文来解释发生了什么。这对你来说很容易——是你写的。解释一下你从OPs代码中改变了什么,以及为什么这个改变会起作用。代码不错,但是你错过了决定他们是否需要睡眠的要求。具体来说,OP询问了如何实现if
/else
Vs.函数。您做了什么?它为什么有效?解释你的answer@Martin由于命名变量、类、类型和注释Serrr、no;没有“自我解释代码”这样的东西,否则它将不会被称为代码,您也不需要注释。评论非常有用;在你的答案中用评论和散文来解释发生了什么。这对你来说很容易——是你写的。解释一下你从OPs代码中改变了什么,以及为什么这个改变会起作用。代码不错,但是你错过了决定他们是否需要睡眠的要求。具体而言,OP询问如何实现if
/else
Vs.函数。