下面是代碼:如何訪問子類中的基本屬性?
<? php
class Animal
{
public $type = 'Animal';
function printProperties()
{
echo $this->type;
}
}
class Dog extends Animal
{
public $type = 'Dog';
function printProperties()
{
echo $this->type.'<br>';
parent::printProperties();
}
$aDog = new Dog;
$aDog-> printProperties();
?>
代碼打印Dog newl Dog
我預計輸出爲Dog newline Animal
。如何得到這個結果。如果覆蓋屬性,我不能訪問achild類中的基本屬性嗎?
這是不可讀的 – Jah
你在第二類中覆蓋'$ type',所以'Dog
Dog'是預期的。 – Rasclatt
因此,如果我覆蓋父屬性,沒有辦法訪問子類中的基本屬性? – user5241471