問題
設計一個person類,條件如下:
1)定義protected屬性:name(姓名)、age(年齡)、sex(性別)
2)定義static靜態屬性:num(用于計算已實體化的人數)
3)定義建構式,實作在物件創建時輸出“I am a person”,并對num加1;
4)定義解構式,在物件銷毀時輸出“bye”;
5)定義共有方法setInfo($info),通過引數設定name、age、sex等屬性資訊
(提示:引數可以為陣列,陣列里包含每個引數的資訊,如$info[name])
6)定義共有方法getInfo(),用于輸出物件的屬性資訊
7)定義共有方法getNum(),用于計算并回傳已實體化的人數
B、設計一個student類,條件如下:
1)繼承person類
2)定義建構式,呼叫父類建構式,并在輸出“I am a student”
3)定義私有屬性:number(學號)、class(班級)、major(專業)
4)多載父類的setInfo($info)方法,設定其為final方法,呼叫父類的setInfo函式以設定共有的屬性資訊,并繼續設定本類的屬性資訊
5)多載父類的getInfo()方法,設定其為final方法,呼叫父類的setInfo函式以輸出共有屬性資訊,并繼續輸出本類的屬性資訊
C、設計一個teacher類,條件如下:
1)繼承自person類;
2)定義建構式,呼叫父類建構式,并在輸出“I am a teacher”
3)定義私有屬性:id(編號)、t_class(授課班級)、department(院系)
4)多載父類的setInfo($info)方法,設定其為final方法,呼叫父類的setInfo函式以設定共有的屬性資訊,并繼續設定本類的屬性資訊
5)多載父類的getInfo()方法,設定其為final方法,呼叫父類的setInfo函式以輸出共有屬性資訊,并繼續輸出本類的屬性資訊
D、實體化student類,呼叫setInfo()函式設定學生資訊(以你個人的真實資訊為參考),然后呼叫getInfo()函式輸出資訊
E、實體化teacher類,呼叫setInfo()函式設定學生資訊(以你個人的真實資訊為參考),然后呼叫getInfo()函式輸出資訊
F、呼叫person類的靜態方法getNum(),輸出已實體化的人員數量
代碼如下:
<?php
class Person
{
protected $name, $age, $sex; //姓名,年齡,性別
public static $num;
public function __construct($name = "", $age = "", $sex = "", $num = "")
{
echo "I am a person!<br>";
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
self::$num++;
}
public function __destruct()
{
echo "bye";
}
public function setInfo($name, $age, $sex, $num)
{
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
$this->num = $num;
}
public function getInfo()
{
echo "姓名:" . $this->name . "<br>年齡:" . $this->age . "<br>性別:" . $this->sex . "<br>";
return self::$num;
}
public function getNum()
{
echo "<br>已實體化的人員數量是:" . self::$num . "<br>";
}
}
Person::$num = 0;
class student extends Person
{
private $number, $clas, $major; //學號 班級 專業
public function __construct($number = "", $clas = "", $major = "")
{
$this->number = $number;
$this->clas = $clas;
$this->major = $major;
echo "I am a student<br>";
}
public final function setInfo($name, $age, $sex, $number, $clas, $major)
{
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
$this->number = $number;
$this->clas = $clas;
$this->major = $major;
}
public final function getInfo()
{
parent::getInfo();
echo "學號:" . $this->number . "<br>班級:" . $this->clas . "<br>專業:" . $this->major . "<br><br>";
}
public function showNum()
{
echo parent::getNum();
}
}
class teacher extends Person
{
private $id, $t_class, $department; //工號 授課班級 院系
public function __construct($id = "", $t_class = "", $department = "")
{
$this->id = $id;
$this->t_class = $t_class;
$this->department = $department;
echo "I am a teacher<br>";
}
public final function setInfo($name, $age, $sex, $id, $t_class, $department)
{
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
$this->id = $id;
$this->t_class = $t_class;
$this->department = $department;
}
public final function getInfo()
{
parent::getInfo();
echo "編號:" . $this->id . "<br>授課班級:" . $this->t_class . "<br>院系:" . $this->department . "<br><br>";
}
public function showNum()
{
echo parent::getNum();
}
}
$s1 = new student();
$s1->setInfo("陳真", 22, "男", 20170521, "計科1701", "計算機科學與技術");
echo $s1->getInfo();
$t1 = new teacher();
$t1->setInfo("王獨秀", 26, "女", 1007, "計科1701、計科1702、計科1703、計科1704", "資訊化學院");
echo $t1->getInfo();
$p1 = new Person();
$p2 = new Person();
$p3 = new Person();
Person::getNum();
運行結果如下:
I am a student
姓名:陳真
年齡:22
性別:男
學號:20170521
班級:計科1701
專業:計算機科學與技術
I am a teacher
姓名:王獨秀
年齡:26
性別:女
編號:1007
授課班級:計科1701、計科1702、計科1703、計科1704
院系:資訊化學院
I am a person!
I am a person!
I am a person!
已實體化的人員數量是:3
byebyebyebyebye
注意
本案例可能并非完全解,只是通過個人在對案例的理解情況下進行撰寫,如有錯誤或者復制運行不對的情況,懇請各位前輩能夠指點一二,以便推動共同進步,謝謝!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/43152.html
標籤:PHP
