- <?php
-
class CartLine
-
{
-
public $price;
-
public $num;
-
-
public function total()
-
{
-
return $this->num * $this->price;
-
}
-
}
-
-
class Cart
-
{
-
protected $sale = 0.8;
-
protected $lines = array();
-
-
public function addLine($cart)
-
{
-
$this->lines[] = $cart;
-
}
-
-
public function calcTotal()
-
{
-
$total = 0;
-
foreach ($this->lines as $line) {
-
$total+= $line->total();
-
}
-
$total = $this->calcSaleTotal($total);
-
return $total;
-
}
-
-
public function calcSaleTotal($amount)
-
{
-
return $amount * $this->sale;
-
}
-
}
-
-
$obj1 = new CartLine();
-
$obj1->price = 100;
-
$obj1->num = 2;
-
$obj2 = new Cart();
-
$obj2->addLine($obj1);
-
echo $obj2->calcTotal();
忘记一切面向对象,重新学习,体会了一下分块设计原则手写了一下,购物车的打折设计