批改状态:合格
老师批语:应用场景 , 是根据项目而定的
实例演示子类的三个应用场景(1.代码复用 2.功能扩展 3.方法重写)

<?php
namespace _0929;
class Demo3
{
// 变量给它一个逼格高的名称:属性
public $product;
public $price;
// 构造方法 __construct 又叫模式方法
// 魔术方法不需要用户手动调用,是有PHP根据某种条件自动触发
public function __construct($product, $price)
{
// 1. 初始化对象的状态,有属性决定
$this->product = $product;
$this->price = $price;
}
// 方法(函数)
public function getInfo()
{
return '商品名称: ' . $this->product.', 商品价格: ' . $this->price;
}
}
// 子类
class Sub1 extends Demo3
{
}
$sub1 = new Sub1('iphone 11','9800');
echo $sub1->getInfo(). '<hr>';
// 子类扩展父类的功能
class Sub2 extends Demo3
{
// 添加一个数量
public $num;
// 构造方法
public function __construct($product, $price, $num)
{
// $this->product = $product;
// $this->price = $price;
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>';
// 子类
// 3. 方法重写 打折
class Sub3 extends Sub2
{
// 重写total()
public function total()
{
$total = parent::total();
// 设置折扣率
switch (true)
{
case ($total < 2000 && $total > 40000):
$discountRate = 0.88;
break;
case ($total >= 40000 && $total < 60000):
$discountRate = 0.78;
break;
case ($total >= 60000):
$discountRate = 0.68;
break;
default:
// 小于或等于2000,不打折
$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();点击 "运行实例" 按钮查看在线实例
限制符使用场景实例

<?php
namespace _0930;
// 访问控制符 public
// public: 类中,类外均可访问
// protected: 类中,类外不可访问, 但是子类中可以访问
// private: 只允许在类中访问,类外和子类中不可以访问
use _0929\Sub3;
class Demo4
{
// 类中成员: 属性, 方法
// 成员属性, 成员方法
// 对象属性: 需要使用类的实例进行访问的成员属性
public $name; // 姓名
protected $position; //职位
private $salary; // 工资
protected $department; // 部门
//
public function __construct($name, $position, $salary,$department)
{
$this->name = $name;
$this->position = $position;
$this->salary = $salary;
$this->department = $department;
}
// 职位访问器/方法/函数
public function getPosition()
{
return $this->position === '开发部' ? $this->name : '无权查看';
}
// 工资访问器/方法/函数
public function getSalary()
{
// 工资 只允许财务部的人看
// if ($this->department === '财务部'){
// return $this->salary;
// }
// return '无权查看';
return $this->department === '财务' ? $this->salary : '无权查看';
}
// 部门访问器/方法/函数
public function getDepartment()
{
return $this->department;
}
}
$obj = new Demo4('朱老师','开发部',8888,'老师');
echo $obj->name,'<br>';
//echo $obj->position,'<br>';
echo $obj->getPosition(),'<br>';
echo $obj->getSalary(),'<br>';
echo $obj->getDepartment(),'<br>';
class Sub extends Demo4
{
public function display()
{
// return $this->name; // name是public类型的
return $this->position; // position是protected类型的
// return $this->salary; // salary是private型的
}
}
echo '<hr>';
$sub = new Sub('欧旺客','讲师', 9999, '培训部');
echo $sub->display(), '<br>';点击 "运行实例" 按钮查看在线实例
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号