请选择 进入手机版 | 继续访问电脑版

PHP面向对象中继承案例分析调用教程

[复制链接]
查看2786 | 回复1 | 2019-3-25 11:10 | 显示全部楼层 |阅读模式
PHP面向对象中继承案例分析调用教程
  1. <?php
  2. //继承用关键词extends来声明
  3. class SportObject{
  4.         public $name;
  5.         public $age;
  6.         public $sex;
  7.         public $avoirdupois;
  8.         
  9.         function __construct($name,$age,$avoirdupois,$sex){
  10.                 $this->name = $name;
  11.                 $this->age = $age;
  12.                 $this->sex = $sex;
  13.                 $this->avoirdupois =$avoirdupois;
  14.                
  15.         function showMe(){
  16.                 echo '自丢网www.zidiu.com';
  17.         }
  18.         }
  19. }

  20. class BeatBasketBall extends SportObject{
  21.         public $height;
  22.         function __construct($name,$height){
  23.                 $this->height = $height;
  24.                 $this->name = $name;
  25.         }
  26.         
  27.         function showMe(){
  28.                 if($this->height > 180){
  29.                         echo $this->name.'符合打篮球。'.'<br>';
  30.                 }else{
  31.                         echo $this->name.'不符合打篮球。'.'<br>';
  32.                 }
  33.         }
  34.         }

  35. class JuZhong extends SportObject{
  36.         function showMe(){
  37.                 if($this->avoirdupois < 85){
  38.                 return $this->name.'符合举重。'.'<br>';
  39.         }else{
  40.                 return $this->name.'不符合举重。';
  41.         }
  42.         }
  43. }

  44. $BBB = new BeatBasketBall('排球','185');
  45. $BBB->showMe().'<br>';

  46. $JZ = new JuZhong('自丢','20','100','男');
  47. echo $JZ->showMe();
复制代码
PHP源码运行效果图:

PHP面向对象中继承案例分析调用教程

PHP面向对象中继承案例分析调用教程

老虎 | 2019-8-20 06:14 | 显示全部楼层
技术学习教程网,支持一下。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

UID
1
贡献
387
丢币
38902
主题
4607
回帖
116
注册时间
2018-9-25
最后登录
2024-3-24