1. 程式人生 > >php類繼承設計商品類手機類圖書類

php類繼承設計商品類手機類圖書類

設計如下幾個類:商品類,有名稱,有價錢,庫存數,可顯示自身資訊(名稱,價錢,庫存)。手機類,是商品的一種,並且有品牌,有產地,可顯示自身資訊。圖書類,是商品的一種,有作者,有出版社, 可顯示自身資訊。建立一個手機物件,並顯示其自身資訊;建立一個圖書物件,並顯示其自身資訊;擴充套件要求:儘量體現封裝性就更好了。

<?php

class  goods{
public $name;
public $money;
public $inventory;
public function __construct($name,$money,$inventory){
$this->name=$name;
$this->money=$money;
$this->inventory=$inventory;
}
public function show()
{
echo '名稱'.$this->name.'價格'.$this->money.'庫存'.$this->inventory;
}
}


class phone extends goods{
public $brand;
public $place;
public function __construct($name,$money,$inventory,$brand,$place){
$this->name=$name;
$this->money=$money;
$this->inventory=$inventory;
$this->brand=$brand;
$this->place=$place;
}
public function show(){
parent::show();
echo '品牌'.$this->brand.'場地'.$this->place;
}
}


class books extends goods{
public  $author;
public $press;
public function __construct($name,$money,$inventory,$author,$press){
$this->name=$name;
$this->money=$money;
$this->inventory=$inventory;
$this->author=$author;
$this->press=$press;
}
public function show(){
parent::show();
echo'作者'.$this->author.'出版社'.$this->press;
}
}


$obj=new phone('手機',750,35,'諾基亞','北京');
$obj->show();
echo'<br>';
$ob=new books('圖書',750,35,'一本道','光華');
$ob->show();