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 ?>