php类的继承,重载,扩展

 

 1 <?php
 2 //1.父类中用public/protected访问修饰符的方法或者变量可以在子类中继承和扩展和操作
 3 //2.其中protected访问修饰符标志的仅用在子类中进行继承和扩展,不能外部访问。
 4 //3.重载父类的方法,不能使用更严格的访问修饰符,即如果父类用public,子类不能用用private和protected。
 5 //4.重载的方法参数数量可以和父类的不一致,但是方法名称必须一样。
 6 
 7 class Human{
 8     //私有属性可以被继承,但是不可以修改操作.
 9     private $buluo;
10     //用于继承的属性,可以修改操作
11     protected $weight,$height;
12     
13     //构造函数
14     public function __construct($buluo,$height,$weight){
15         $this->buluo = $buluo;
16         $thhis->height = $height;
17         $this->weight = $weight;
18     }
19     //用于重载的方法
20     public function cry(){
21         echo '555 and I am from '.$this->buluo.'.<br/>';       
22     }  
23     //用于继承的方法
24     public function smile(){
25         echo 'I am smiling.<br/>';
26     } 
27 }
28 
29 class Stu extends Human{
30     //私有属性
31     private $sn,$name;
32     
33     //构造函数重载,参数可变
34     public function __construct($name,$sn,$height,$weight){
35         //扩展类的私有属性
36         $this->name = $name;
37         $this->sn = $sn;
38         //继承属性,直接当成扩展类的用,如果没有修改访问修饰符,应该和父类一致。
39         $this->height = $height;
40         $this->weight = $weight;
41     }
42     //普通方法重载
43     public function cry($type){
44         $cry_type=array('xiaoshengku','dashengku');
45         if(!in_array($type,$cry_type)){
46             return false;
47         }
48         echo $this->name.' is '.$type.'.<br/>';
49     }
50     //扩展方法
51     public function learn(){
52         echo 'good good study,day day up.<br/>';
53     }
54     public function introduce(){
55         echo 'My name is : '.$this->name.'<br/>';
56         echo 'My sn is : '.$this->sn.'<br/>';
57         echo 'My height is : '.$this->height.'<br/>';
58         echo 'My weight is : '.$this->weight.'<br/>';
59     }
60     //继承方法
61     //smile:但是不用写出来
62 }
63 ?>

 

转载于:https://www.cnblogs.com/xtyang/p/3229291.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值