1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| <?php
class Demo { public $product; public $price;
public function __construct($product, $price) { $this->product = $product; $this->price = $price; }
public function getInfo() { return '商品名称: ' . $this->product.', 商品价格: ' . $this->price; } }
class Sub1 extends Demo { }
$sub1 = new Sub1('iPhone 11', 9800); echo $sub1->getInfo() . '<hr>';
class Sub2 extends Demo { public $num;
public function __construct($product, $price, $num) {
parent::__construct($product, $price); $this->num = $num; }
public function total() { return round($this->price * $this->num, 3); } }
$sub2 = new Sub2('电脑', 3980.1234, 13); echo $sub2->product . '的总价是: '. $sub2->total(). '<hr>';
class Sub3 extends Sub2 { public function total() { $total = parent::total();
switch (true) { case ($total > 20000 && $total < 40000): $discountRate = 0.88; break; case ($total >= 40000 && $total < 60000): $discountRate = 0.78; break; case ($total >= 60000): $discountRate = 0.68; break; default: $discountRate = 1; } $discountPrice = round($total*$discountRate, 2);
if ($discountRate < 1) { $discountPrice=$discountPrice . '元, <span style="color: red">('. $discountRate.'折)</span>'; }
return $discountPrice; } }
$sub3 = new Sub3('电脑', 3980, 13); $sub3 = new Sub3('电脑', 3980, 33);
echo '折扣价是: ' . $sub3->total();
?>
|