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
中百
<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
中百
相关文章推荐
- PHP没有多继承的特性
- PHP 使用 mcrypt 扩展中的 mcrypt_encrypt() 和 mcrypt_decrypt() 对数据进行加密和解密
- php 面向对象编程的高级技术
- php 类的应用实例
- get_set.php 类的访问控制
- 使用类的属性 get_set.php
- php简单备份与还原MySql的方法
- php header中的Cache-control参数说明
- php 创建类
- thinkphp笔记
- php 其他常用文件处理函数
- PHP 中的文件处理技术
- php简单复制文件的方法
- php跨域
- yii学习笔记
- win10环境PHP 7 安装配置【教程】
- urldecode.php
- ubuntu php编译安装 openssl/evp.h: 没有那个文件或目录
- PHP培训实战教程之PHP函数
- PHP之误解