摘要:<?php /** * 延迟绑定并在父类中调用子类中重写的静态方法 */ class Father { //静态属性 public static $money = 30000;
<?php /** * 延迟绑定并在父类中调用子类中重写的静态方法 */ class Father { //静态属性 public static $money = 30000; //静态方法获得类名 public static function getClass() { //返回当前的类名 return __CLASS__; } //静态方法访问当前类中的静态成员 public static function getMoney() { //self不能实现延迟绑定 //return self::$money; //后期延迟绑定要用static来修饰,可以在执行后来绑定,而不在调用父类中的数据 return static::$money; } } //定义 子类 继承于父类 class Son extends Father { //重新写静态属性工资 public static $money = 50000; //获得当前的类名 public static function getClass() { return __CLASS__; } } //$father = new Father(); echo Father::getClass().' 类工资是:'.Father::$money.'<br>'; echo Father::getClass().' 类工资是:'.Father::getMoney().'<br>'; echo Son::getClass().' 类工资中:'.Son::$money.'<br>'; echo Son::getClass().' 类工资中:'.Son::getMoney().'<br>'; echo Father::getClass().' 类调用子类'.Son::getClass().'工资:'.Son::getMoney().'<br>';
批改老师:查无此人批改时间:2019-04-18 10:05:49
老师总结:完成的不错。类学习完,你就php入门了。继续加油。