返回完成后期静态绑......登陆

完成后期静态绑定技术,实现在父类调用子类重定的静态成员

҈果҈果҈果҈ ҈ ҈2019-04-11 05:14:05201
<?php
class Car{
    public static $speed = 120;
    public static $fuel = 100;
    public static $color ='红色';
    public static $brand = '五菱宏光';
    public static function move()
    {
       return '  正在以'.static::$speed .'的速度行驶,油量为:'. static::$fuel; 
    }
    public static function state()
    {
        return '有辆牌子为'.static::$brand.static ::move();
    }
}
class Supercar extends Car
{
    public static $speed = 240;
    public static $fuel = 200;
    public static $brand = '五菱宏光S';

    public static function move()
    {       
        return '颜色为'.static::$color. ' 正在以'.static::$speed .'的速度行驶,油量为:'. static::$fuel; 
    }
}
$car = new Car;
echo $car::state().'<hr>';
$supercar = new Supercar;
// echo $supercar::move().'<hr>';
echo $supercar::state().'<hr>';
echo $car::state().'<hr>';
?>

car.jpg

经过本章节的学习,对后期静态绑定重载技术比较深入的了解,在子类静态成员中如果不重新赋值即继承了父类的静态成员的值,当对父类的静态成员进行覆写即是更新了继承的成员值进行改变。从而达到代码的复用性和让子类的功能更加灵活、多元化。

最新手记推荐

• 用composer安装thinkphp框架的步骤• 省市区接口说明• 用thinkphp,后台新增栏目• 管理员添加编辑删除• 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消回复发送