时间:2021-07-01 10:21:17 帮助过:2人阅读
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
");
}
//FALSE
if($w1->equals($w3))
{
print("w1 and w3 are the same
");
}
//FALSE, == includes id in comparison
if($w1 == $w2) //不等,因为ID不同
{
print("w1 and w2 are the same
");
}
?> 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
");
}
//print Cog 输出 Cog
print($w2->getName());
?> http://www.bkjia.com/PHPjc/532555.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/532555.htmlTechArticle第八节--访问方式 PHP5的访问方式允许限制对类成员的访问. 这是在PHP5中新增的功能,但在许多面向对象语言中都早已存在. 有了访问方式,才...