<?php
/*
?* WEB开发笔记 www.chhua.com 每日练习之 继承与重载
?*/
class Product {//基类
?public $name;
?public $type;
?
?public function __construct($name,$type){
??$this->name=$name;
??$this->type=$type;
?}
?
?public function get_zhaiyao(){
??$zhaiyao=$this->name.”({$this->type})”;
??return $zhaiyao;
?}
}

class BookProduct extends Product {//继承基类
?public $numpages;
?
??? public function __construct($name,$type,$numpages=0){
??$this->name=$name;
??$this->type=$type;
??$this->numpages=$numpages;
?}
?public function getPages(){//独有方法
??return $this->numpages;
?}
?
?public function get_zhaiyao(){//重载方法
??$zhaiyao=parent::get_zhaiyao();//调用父类的方法
??$zhaiyao.=”,Page Count-{$this->numpages}页”;
??return $zhaiyao;
?}
}

class CdProduct extends Product{//继承基类
?public $playlength;
?
public function __construct($name,$type,$playlength=0){
??$this->name=$name;
??$this->type=$type;
??$this->playlength=$playlength;
?}
?public function getTimes (){//独有方法
??return $this->playlength;
?}
?
?public function get_zhaiyao(){//重载方法
??$zhaiyao=parent::get_zhaiyao();//调用父类的方法
??$zhaiyao.=”Play Length-{$this->playlength}分”;
??return $zhaiyao;
?}
}

$cd=new CdProduct(“《经典教程》”,”CD”,120);
$book=new BookProduct(“《php教程》”,”BOOK”,456);

echo $cd->get_zhaiyao();
echo “<br>”;
echo $book->get_zhaiyao();
?>

自由转载,转载请注明: 转载自WEB开发笔记 www.chhua.com

本文链接地址: PHP类的继承与重载 http://www.chhua.com/web-note717

相关笔记

更多