OOP的第二大特性--继承性
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OOP的第二大特性--继承性相关的知识,希望对你有一定的参考价值。
1,类继承的应用
子类继承父类,拥有父类的属性和方法.
代码:
1 <?php 2 header("Content-Type:text/html;charset=utf-8"); 3 4 //声明一个人类 5 6 class Person 7 { 8 //属性 9 public $sex; 10 public $name; 11 public $age; 12 public $height; 13 //方法 14 public function eat() 15 { 16 echo "我会吃!<br>"; 17 } 18 19 public function sleep() 20 { 21 echo "我能睡!<br>"; 22 } 23 } 24 25 //男人类 26 class Man 27 { 28 //属性 29 public $name; 30 public $age; 31 public $sex = ‘男‘; 32 public $height; 33 public $hou; 34 35 //方法 36 public function eat() 37 { 38 echo "我会吃!<br>"; 39 } 40 41 public function sleep() 42 { 43 echo "我能睡!<br>"; 44 } 45 46 public function peeStand() 47 { 48 echo "我会站着尿尿<br>"; 49 } 50 } 51 52 //定一个超级英雄类 53 class SuperMan 54 { 55 //属性 56 public $name; 57 public $age; 58 public $sex = ‘男‘; 59 public $height; 60 public $hou; 61 public $energy; 62 63 //方法 64 public function eat() 65 { 66 echo "我会吃!<br>"; 67 } 68 69 public function sleep() 70 { 71 echo "我能睡!<br>"; 72 } 73 74 public function peeStand() 75 { 76 echo "我会站着尿尿<br>"; 77 } 78 79 public function fly() 80 { 81 echo "我会飞翔!<br>"; 82 } 83 }
格式:
class 子类名 extends 父类名
{
code...
}
特点:
① 一个父类(基类)可以被多个子类继承
② 一个子类不能同时继承多个父类
2、继承的访问控制
位置 | public | protected | private |
在类的外部 | Y | N | N |
在子类的内部 | Y | Y | N |
在类的内部 | Y | Y | Y |
代码:
<?php header("Content-Type:text/html;charset=utf-8"); //子类重写父类属性 class User { public $name = "马克思"; protected $age = 40; private $money = "一百亿元"; protected function getInfo() { echo "这是父类的get<br>"; echo "name:".$this->name."|age:".$this->age."|money:".$this->money."<br>"; } } class VipUser extends User { public $name = "乔布斯"; //protected $name = "乔布斯"; //public $age = 50; protected $age = 50; public $money = ‘一块钱‘; public function demo() { echo "在子类的内部访问父类的public属性:".$this->name."<br>"; echo "在子类的内部访问父类的protected属性:".$this->age."<br>"; echo "在子类的内部访问父类的private属性:".$this->money."<br>"; } public function getInfo() { echo "这是子类的get<br>"; echo "name:".$this->name."|age:".$this->age."|money:".$this->money."<br>"; } } echo "<pre>"; //实例化 $user = new User(); $vip = new VipUser(); $vip->getInfo(); echo "<hr>"; var_dump($user); var_dump($vip);
输出结果:
3、子类中重写父类方法
① 子类中可以重写父类的属性和方法
② 子类重写非私有方法和属性时 只能更加开放 level更低
③ 重载父类方法 parent::父类方法名()
代码:
<?php header("Content-Type:text/html;charset=utf-8"); //子类重写父类属性 class User { public $name = "马克思"; protected $age = 40; private $money = "十一亿"; public function __construct($name,$age) { $this->name=$name; $this->age = $age; } public function getInfo() { echo "这是父类的get<get>";; echo "name".$this->name."|age:".$this->age."|money:".$this->money."<br>"; } public function demo() { echo "父demo <br>"; } } class VipUser extends User { public $home; private $money; public function __construct($name,$age,$home) { parent::__construct($name,$age); $this->home = $home; } public function getInfo() { echo "这是子类的get<br>"; echo "name:".$this->name."|age".$this->age."|money:".$this->money."<br>"; } public function demo() { parent::demo(); echo "FUNK!!!!"; } } echo "<pre>"; //实例化 $user = new User(‘张三‘,10); $vip = new VipUser(‘李四‘,‘11‘,‘新疆‘); var_dump($user); var_dump($vip); $user->getInfo(); $vip->getInfo(); $vip->demo();
输出结果:
以上是关于OOP的第二大特性--继承性的主要内容,如果未能解决你的问题,请参考以下文章