1
無法解釋爲什麼會打印「\ n1」。私有財產不能被繼承。這就是爲什麼構造函數應該設置新定義的public proverty的值,但「print $ b-> number」不打印任何內容,而「$ b-> printNumber()」打印「1」。在PHP中覆蓋私有財產
class A
{
private $number;
public function __construct($number)
{
$this->number = $number;
}
public function printNumber()
{
print $this->number;
}
}
class B extends A
{
public $number;
}
$b = new B(1);
print $b->number;
print "\n";
$b->printNumber();