1.展示类的继承和静态的方法
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>定义和调用函数</title>
</head>
<body>
<?
class Cart{
var $items;
function Cart(){
$this->Say();
}
function addItem($product,$num){
$this->items[$product]+=$num;
}
function reomveItem($product,$num){
if($this->items[$product]>$num){
$this->items[$product]-=$num;
return true;
}elseif($this->items[$product]==$num){
unset($this->items[$product]);
return true;
}
else{
return false;
}
}
function ShowCart(){
foreach($this->items as $k=>$v){
echo "<br/>
".$k."=".$v;
}
}
function Say(){
echo "<br/> I'm Cart";
}
static function SayHello(){
echo "<br/> Hello World!";
}
}
class AutoCart extends Cart
{
function AutoCart(){
$this->addItem("product1",1);
$this->Say();
}
function ConstructCart($p="11",$n=2){
$this->addItem($p,$n);
}
function Say(){
echo "<br/> I'm AutoCart";
}
}
Cart::SayHello();
$cart = new AutoCart();
$cart->ShowCart();
$cart2=new Cart();
$cart2->addItem("12",2);
$cart2->ShowCart();
?>
</body>
</html>