扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
class Widget { private $name; private $price; private $id; public function __construct($name, $price) { $this->name = $name; $this->price = floatval($price); $this->id = uniqid(); } //checks if two widgets are the same 检查两个widget是否相同 public function equals($widget) { return(($this->name == $widget->name)AND ($this->price == $widget->price)); } } $w1 = new Widget('Cog', 5.00); $w2 = new Widget('Cog', 5.00); $w3 = new Widget('Gear', 7.00); //TRUE if($w1->equals($w2)) { print("w1 and w2 are the same n"); } //FALSE if($w1->equals($w3)) { print("w1 and w3 are the same n"); } //FALSE, == includes id in comparison if($w1 == $w2) //不等,因为ID不同 { print("w1 and w2 are the same n"); } ?> |
class Widget { private $name; private $price; private $id; public function __construct($name, $price) { $this->name = $name; $this->price = floatval($price); $this->id = uniqid(); } //checks if two widgets are the same public function equals($widget) { return(($this->name == $widget->name)AND($this->price == $widget->price)); } protected function getName() { return($this->name); } } class Thing extends Widget { private $color; public function setColor($color) { $this->color = $color; } public function getColor() { return($this->color); } public function getName() { return(parent::getName()); } } $w1 = new Widget('Cog', 5.00); $w2 = new Thing('Cog', 5.00); $w2->setColor('Yellow'); //TRUE (still!) 结果仍然为真 if($w1->equals($w2)) { print("w1 and w2 are the same n"); } //print Cog 输出 Cog print($w2->getName()); ?> |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者