您的位置:首页 > 编程语言 > PHP开发

extends.php

2016-05-09 11:23 465 查看
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

 <head>

  <title> extends.php </title>

  <meta charset="UTF-8">

  <meta name="Author" content="">

  <meta name="Keywords" content="">

  <meta name="Description" content="">

 </head>

 <body>

 <?php

class Cart {

var $items; // 购物车中的物品

var $name;

// 将 $num 个 $artnr 物品加入购物车

function __construct($myName) //构造函数

{

echo("$myName 的购物车,购物过程如下:<br>");

$this->name=$myName;

}

 //添加商品

 function add_item($artnr, $num) {

  if(empty($this->items[$artnr]))

  {

   $this->items[$artnr] = $num;

  }

  else

  {

   $this->items[$artnr] += $num;

  }

 echo "<br>添加 $num 个 $artnr,$artnr 的总数为 " .$this->items[$artnr];

 }

 // 将 $num 个 $artnr 物品从购物车中取出

 function remove_item($artnr, $num) {

  if ($this->items[$artnr] > $num) {

  $this->items[$artnr] -= $num;

  echo "<br>放回 $num 个 $artnr,$artnr 的总数为 ".

  $this->items[$artnr];

  return true;

  } elseif ($this->items[$artnr] == $num) {

  unset($this->items[$artnr]);

  echo "<br>放回 $num 个 $artnr,$artnr 的总数为0";

  return true;

  } else {

  return false;

  }

 }

}

 class location_Cart extends Cart {

 var $location; //子类中声明属性

function set_location ($myloacation) { //子类中声明方法

$this->location = $myloacation;

 }

 }

$myCart =new location_Cart("张三");

$myCart->set_location ("中百");

$myCart->add_item("苹果",9);

echo '<br>',$myCart->location,'<br>' ;

 ?>

 </body>

</html>

张三 的购物车,购物过程如下:

添加 9 个 苹果,苹果 的总数为 9

中百
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: