批改状态:合格
老师批语:总结的很好, php中的访问限制符, 实际上就是类中成员的作用域, 和Java中很类似
一、子类的作用
代码复用
<?php
namespace _001;
//父类
class Myclass{
public $product;
public $price;
public function __construct($product,$price)
{
$this->product=$product;
$this->price=$price;
}
public function getinfo()
{
return '商品名称:'.$this->product.'<br>'.'商品价格:'.$this->price;
}
}
//子类
class Sub1 extends Myclass{
}
$obj=new Sub1('笔记本电脑',8888);
echo $obj->getinfo();点击 "运行实例" 按钮查看在线实例
运行结果如图所示:

2.功能扩展
<?php
namespace _001;
//父类
class Myclass{
public $product;
public $price;
public function __construct($product,$price)
{
$this->product=$product;
$this->price=$price;
}
public function getinfo()
{
return '商品名称:'.$this->product.'<br>'.'商品价格:'.$this->price;
}
}
//子类
class Sub1 extends Myclass{
//在这里我们可以为MYclass里面增加一个属性数量
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);
}
}
$obj=new Sub1('笔记本电脑',8888,7);
echo $obj->getinfo().'<br>'.'商品数量:'.$obj->num.'<br>'.'商品总价格:'.$obj->total();点击 "运行实例" 按钮查看在线实例
运行结果如下图所示:

3.方法重写
<?php
namespace _001;
//父类
class Myclass{
public $product;
public $price;
public function __construct($product,$price)
{
$this->product=$product;
$this->price=$price;
}
public function getinfo()
{
return '商品名称:'.$this->product.'<br>'.'商品价格:'.$this->price;
}
}
//子类
class Sub1 extends Myclass{
//在这里我们可以为MYclass里面增加一个属性数量
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);;
}
}
class Sub2 extends Sub1{
public function total(){
//买满10000元减500元,买满20000元减1200元,买满30000元直接打7折,小于10000元不优惠。
$total=parent::total();
//设置条件判断
switch(true){
case($total>=10000 && $total<30000):
$manjian=500;
break;
case($total>=30000 && $total<40000):
$manjian=1200;
break;
case($total>=40000):
$zk=0.7;
break;
default:
$yj=0;
}
//优惠后的价格
if(isset($manjian)){
$newprice=round(($total-$manjian),0);
$newprice='商品总原价:'.$total.'<br>'.'商品优惠价:'.$newprice.'<br>'.'优惠了'.$manjian.'元';
}
if(isset($zk)){
$newprice=round($total*$zk,0);
$newprice='商品总原价:'.$total.'<br>'.'商品优惠价:'.$newprice.'<br>'.'打了7折';
}
if(isset($yj)){
$newprice=round($total,0);
$newprice='商品总原价:'.$total.'<br>'.'商品优惠价:'.$newprice.'<br>'.'买更多享受优惠吧!';
}
return $newprice;
}
}
$obj=new Sub2('笔记本电脑',8888,7);
echo $obj->getinfo().'<br>'.'商品数量:'.$obj->num.'<br>'.$obj->total();点击 "运行实例" 按钮查看在线实例
运行后效果如下图所示:




<?php
namespace _001;
// 访问控制符: public
// public : 类中,类外均可访问, 子类中也可以访问
// protected: 类中,类外不可访问, 但是子类中可以访问
// private: 只允许在类中, 类外, 子类中不可访问
class Demo{
public $name;
protected $department;
private $salary;
public function __construct($name,$department,$salary){
$this->name=$name;
$this->department=$department;
$this->salary=$salary;
}
//权限控制,只有管理员和财务组的可以看到员工的工资
public function getsalary(){
return $this->department === '管理员'?$this->salary:'无权查看工资';
}
}
$obj=new Demo('张三','非管理员','3000');
echo $obj->getsalary();点击 "运行实例" 按钮查看在线实例

总结:
子类的三个功能:
代码复用 2.功能扩展 3.方法重写
限制符的区别:
public : 类中,类外均可访问, 子类中也可以访问
protected: 类中,类外不可访问, 但是子类中可以访问
private: 只允许在类中, 类外, 子类中不可访问
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号