PHP面向对象中继承案例分析调用教程- <?php
- //继承用关键词extends来声明
- class SportObject{
- public $name;
- public $age;
- public $sex;
- public $avoirdupois;
-
- function __construct($name,$age,$avoirdupois,$sex){
- $this->name = $name;
- $this->age = $age;
- $this->sex = $sex;
- $this->avoirdupois =$avoirdupois;
-
- function showMe(){
- echo '自丢网www.zidiu.com';
- }
- }
- }
- class BeatBasketBall extends SportObject{
- public $height;
- function __construct($name,$height){
- $this->height = $height;
- $this->name = $name;
- }
-
- function showMe(){
- if($this->height > 180){
- echo $this->name.'符合打篮球。'.'<br>';
- }else{
- echo $this->name.'不符合打篮球。'.'<br>';
- }
- }
- }
- class JuZhong extends SportObject{
- function showMe(){
- if($this->avoirdupois < 85){
- return $this->name.'符合举重。'.'<br>';
- }else{
- return $this->name.'不符合举重。';
- }
- }
- }
- $BBB = new BeatBasketBall('排球','185');
- $BBB->showMe().'<br>';
- $JZ = new JuZhong('自丢','20','100','男');
- echo $JZ->showMe();
复制代码 PHP源码运行效果图:
PHP面向对象中继承案例分析调用教程
|
|