杜泰磋宏剧匹来杭迁巨握飞略
新建MobilePhone.php,SmartPhone.php,demo3.php
MobilePhone.php:
<?php /** * 创建手机类:MobilePhone 当做父类(基类) */ class MobilePhone { //访问限定符:protected 受保护的类成员 //只能在本类或者子类中使用的类成员 //public:在外部,内部,子类中都可以使用 //private:仅限在类内部使用 //protected:在本类和子类中使用 protected $brand;//品牌 protected $model;//型号 protected $price;//价格 //构造方法 public function __construct($brand,$model,$price) { $this->brand=$brand; $this->model=$model; $this->price=$price; } public function call()//手机打电话方法 { return "打电话"; } }SmartPhone.php:<?php /** * 创建:智能手机类: SmartPhone * 类SmartPhone(智能手机)继承自MobilePhone(手机) * 1.类继承:在子类上使用关键字extends * 2.php不支持多继承,仅允许从一个父类上继承 * 3.父类又叫超类或基类,通常只提供一些最基本的功能 * 4.子类又叫派生类,可以继承父类中的公共和受保护的成员 * * 子类的功能是用来扩展或重载父类的某些功能 */ class SmartPhone extends MobilePhone//继承手机类 { public function __get($name)//使用查询器 使得在外部可以访问父类的保护属性 { return $this->$name; } }demo3.php:
<?php /* * 类的继承与方法重载 * */ //使用自动加载器来加载类:(简写版) spl_autoload_register(function($className){ require "./class/".$className.".php"; }); //实例化子类 $smartphone=new SmartPhone('apple','iphone8',5888); echo "品牌:".$smartphone->brand."<br>"; echo "型号:".$smartphone->model."<br>"; echo "价格:".$smartphone->price."元<br>";执行:
对子类的属性进行扩充:
修改SmartPhone.php:
<?php /** * 创建:智能手机类: SmartPhone * 类SmartPhone(智能手机)继承自MobilePhone(手机) * 1.类继承:在子类上使用关键字extends * 2.php不支持多继承,仅允许从一个父类上继承 * 3.父类又叫超类或基类,通常只提供一些最基本的功能 * 4.子类又叫派生类,可以继承父类中的公共和受保护的成员 * * 子类的功能是用来扩展或重载(重写)父类的某些功能 */ class SmartPhone extends MobilePhone//继承手机类 { public function __get($name)//使用查询器 使得在外部可以访问父类的保护属性 { return $this->$name; } //1.对父类属性进行扩展,增加新的特征,如果不在子类中使用,推荐设置为private private $camera=false;//是否有拍照功能 private $internet=false;//是否有上网功能 //必须使用构造方法对使用当前新增属性生效 public function __construct($brand, $model, $price,$camera,$internet) { // 父类中的构造器的属性初始化 $this->brand=$brand; $this->model=$model; $this->price=$price; // 子类中的构造器的属性初始化 $this->camera=$camera; $this->internet=$internet; } }demo3.php
<?php /* * 类的继承与方法重载 * */ //使用自动加载器来加载类:(简写版) spl_autoload_register(function($className){ require "./class/".$className.".php"; }); //实例化子类 $smartphone=new SmartPhone('apple','iphone8',5888,true,true); echo "品牌:".$smartphone->brand."<br>"; echo "型号:".$smartphone->model."<br>"; echo "价格:".$smartphone->price."元<br>"; echo "照相:".($smartphone->camera ? '支持':'不支持')."<br>"; echo "上网:".($smartphone->internet ? '支持':'不支持')."<br>";执行:
简化SmartPhone.php中的构造方法:
public function __construct($brand, $model, $price,$camera,$internet) { // 父类中的构造器的属性初始化 parent::__construct($brand, $model, $price); // 子类中的构造器的属性初始化 $this->camera=$camera; $this->internet=$internet; }
增加子类的方法
SmartPhone.php
<?php /** * 创建:智能手机类: SmartPhone * 类SmartPhone(智能手机)继承自MobilePhone(手机) * 1.类继承:在子类上使用关键字extends * 2.php不支持多继承,仅允许从一个父类上继承 * 3.父类又叫超类或基类,通常只提供一些最基本的功能 * 4.子类又叫派生类,可以继承父类中的公共和受保护的成员 * * 子类的功能是用来扩展或重载(重写)父类的某些功能 */ class SmartPhone extends MobilePhone//继承手机类 { public function __get($name)//使用查询器 使得在外部可以访问父类的保护属性 { return $this->$name; } //1.对父类属性进行扩展,增加新的特征,如果不在子类中使用,推荐设置为private private $camera=false;//是否有拍照功能 private $internet=false;//是否有上网功能 //必须使用构造方法对使用当前新增属性生效 public function __construct($brand, $model, $price,$camera,$internet) { // 父类中的构造器的属性初始化 parent::__construct($brand, $model, $price); // 子类中的构造器的属性初始化 $this->camera=$camera; $this->internet=$internet; } //增加子类的新功能 public function game() { return "打游戏"; } }demo3.php
<?php /* * 类的继承与方法重载 * */ //使用自动加载器来加载类:(简写版) spl_autoload_register(function($className){ require "./class/".$className.".php"; }); //实例化子类 $smartphone=new SmartPhone('apple','iphone8',5888,true,true); echo "品牌:".$smartphone->brand."<br>"; echo "型号:".$smartphone->model."<br>"; echo "价格:".$smartphone->price."元<br>"; echo "照相:".($smartphone->camera ? '支持':'不支持')."<br>"; echo "上网:".($smartphone->internet ? '支持':'不支持')."<br>"; echo $smartphone->call()."<br>"; echo $smartphone->game()."<br>";执行:
重写父类中的方法:
SmartPhone.php
<?php /** * 创建:智能手机类: SmartPhone * 类SmartPhone(智能手机)继承自MobilePhone(手机) * 1.类继承:在子类上使用关键字extends * 2.php不支持多继承,仅允许从一个父类上继承 * 3.父类又叫超类或基类,通常只提供一些最基本的功能 * 4.子类又叫派生类,可以继承父类中的公共和受保护的成员 * * 子类的功能是用来扩展或重载(重写)父类的某些功能 */ class SmartPhone extends MobilePhone//继承手机类 { public function __get($name)//使用查询器 使得在外部可以访问父类的保护属性 { return $this->$name; } //1.对父类属性进行扩展,增加新的特征,如果不在子类中使用,推荐设置为private private $camera=false;//是否有拍照功能 private $internet=false;//是否有上网功能 //必须使用构造方法对使用当前新增属性生效 public function __construct($brand, $model, $price,$camera,$internet) { // 父类中的构造器的属性初始化 parent::__construct($brand, $model, $price); // 子类中的构造器的属性初始化 $this->camera=$camera; $this->internet=$internet; } //增加子类的新功能 public function game() { return "打游戏"; } //重写父类中的call()方法 //类方法的重写:重载 更多的时候是进行功能的拓展 public function call() { return "看视频"; } }demo3.php
<?php /* * 类的继承与方法重载 * */ //使用自动加载器来加载类:(简写版) spl_autoload_register(function($className){ require "./class/".$className.".php"; }); //实例化子类 $smartphone=new SmartPhone('apple','iphone8',5888,true,true); echo "品牌:".$smartphone->brand."<br>"; echo "型号:".$smartphone->model."<br>"; echo "价格:".$smartphone->price."元<br>"; echo "照相:".($smartphone->camera ? '支持':'不支持')."<br>"; echo "上网:".($smartphone->internet ? '支持':'不支持')."<br>"; echo $smartphone->call()."<br>"; echo $smartphone->game()."<br>";执行,可以看到子类中修改父类的call()方法成功:
拓展父类中的call()方法:
修改SmartPhone.php中的call()方法:
public function call() { return parent::call().",同时还能听音乐,看视频"; }执行:
迟透闪腻承附恰烧宝砰场舜冻