php静态类使用场景,实例演示子类的三个应用场景, 实例演示类成员的三种访问限制符的使用场景2019/9/30...

子类的三种应用场景<?php

//类的继承: 代码复用的重要手段

namespace _0930;

class Demo3

{

// 属性(变量)

public $product;//商品名称

public $price;//商品价格

// 构造方法

public function __construct($product, $price)

{

$this->product = $product;

$this->price = $price;

}

// 方法(函数)

public function getInfo()

{

return '商品名称: ' . $this->product.', 商品价格: ' . $this->price;

}

}

// 子类

// 1. 代码复用

class Sub1 extends Demo3

{

// ...

}

$sub1 = new Sub1('iPhone 11', 9800);

echo $sub1->getInfo() . '


';  //getInfo将整个数据打印输出

// 子类

// 2. 功能扩展

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); // 3就是参数就是保留的小数点

}

}

$sub2 = new Sub2('电脑', 3980.1234, 13);

echo $sub2->product . '的总价是: '. $sub2->total(). '


';

// 子类

// 3. 方法重写   //满足个性化需求

class Sub3 extends Sub2

{

// 重写total()

public function total()

{

$total = parent::total();   //调用原来未被重写的方法第56行  roud以后

//        设置折扣率

switch (true)

{

case ($total > 20000 && $total 

$discountRate = 0.88;   //折扣率discountRate

break;

case ($total >= 40000 && $total 

$discountRate = 0.78;

break;

case ($total >= 60000):

$discountRate = 0.68;

break;

default:

// 小于或等于2000,不打折

$discountRate = 1;

}

// 打折后的价格

$discountPrice = round($total*$discountRate, 2);     //折扣价的变量$discountPrice

if ($discountRate 

$discountPrice=$discountPrice . '元, ('. $discountRate.'折)';

}

// 返回折扣价

return $discountPrice;

}

}

$sub3 = new Sub3('电脑', 3980, 13);

$sub3 = new Sub3('电脑', 3980, 33);

echo '折扣价是: ' . $sub3->total();

echo '


';

$sub3 = new Sub3('电脑', 2990, 10);

echo '折扣价是: ' . $sub3->total();

echo '


';

--------------------------------------------------------------------------------------------------------------

运行效果图

99a67a7bbae8e476b036dbf509f9cdc7.png

实例演示类成员的三种访问限制符的使用场景<?php

namespace _0930;

// 访问控制符: public

// public : 类中,类外均可访问, 子类中也可以访问

// protected: 类中,类外不可访问, 但是子类中可以访问

// private: 只允许在类中, 类外, 子类中不可访问

//访问控制符: public

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;

}

//工资访问器/方法/函数

public function getSalary()

{

//工资 只允许财务部的人看

// if ($this->department === '财务部')

// {

// return $this->salary;

// }

// return '无权查看';

//--------------低下是简化版-----------------

return $this->department === '财务部' ? $this-> salary : '无权查看';  //?前面是条件:前面代表成功访问: 代表失败返回.

}

//部门访问器/方法/函数

public function getDepartment()

{

return $this->department;

}

}

$obj = new Demo4('万物', '学生', 1500, '研发部');

echo $obj->name,'
';

//echo $obj->position,'
';

echo $obj->getPosition(),'
';

//echo $obj->salary;

echo $obj-> getSalary(),'
';

// echo $obj->department;

echo $obj->getDepartment(),'
';

class Sub extends Demo4

{

public function display()

{

// return $this->position;// protected

// return $this->salary;//private

return $this->name;//public

}

}

$sub = new Sub('舞','舞者',6666,'国家队');

echo $sub->display(),'
';

?>

3501eeb569d997e823b8dcc731507406.png

----------------------------------------------------------------------------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值