无法查看从父类继承的属性 [重复]

Posted

技术标签:

【中文标题】无法查看从父类继承的属性 [重复]【英文标题】:Unable to view property inherited from parent class [duplicate] 【发布时间】:2019-08-11 14:02:47 【问题描述】:

我创建了一个带有以下变量的银行账户类,$Balance 变量是私有的,所以我使用 setter 和 getter 函数来访问 balance 的私有属性。

class BankAccount

    public $HoldersName;
    public $AccountNumber;
    public $SortCode;
    private $Balance = 1;
    public $APR = 0;
    public $Transactions = []; //array defined.

    public function __construct($HoldersName, $AccountNumber, $SortCode, $Balance = 1) //constructor where i.e. HoldersName is the same as the variable $HoldersName. 
                                                                                      //constructor automatically called when object is created.
        echo "Holders Name: " . $this->HoldersName  = $HoldersName . "</br>";
        echo "Account Number: " . $this->AccountNumber = $AccountNumber . "</br>";
        echo "Sort Code: " . $this->SortCode = $SortCode . "</br>";
        $this->Balance = $Balance >= 1 ? $Balance : 1;
    


    public function set_Balance($Balance) //SET balance.
    
        return $this->Balance=$Balance;
    

    public function get_Balance() //GET balance.
    
        return $this->Balance; //Allows us to access the prviate property of $Balance.
    


SavingsAccount 类扩展了 BankAccount,因此它继承了 BankAccount 类的所有内容。我创建了一个函数来计算利息,即余额 6800 * (0.8+0.25) * 1。

class SavingsAccount extends BankAccount

    public $APR = 0.8;
    public $APRPaymentPreference;
    public $ExtraBonus = 0.25;

    public function CalculateInterest()
    
        $this->Balance = $this->Balance * ($this->APR + $this->ExtraBonus) * 1; //this is where i get the error now

    

    public function FinalSavingsReturn()
    

    

这里我创建了一个SavingsAccount类的实例,余额为6800,我尝试调用函数SavingsAccount::CalculateInterest()却出现了这个错误:

注意未定义的属性:第 42 行的 SavingsAccount::$Balance

//define objects
$person2 = new SavingsAccount ('Peter Bond', 987654321, '11-11-11', 6800); //create instance of class SavingsAccount
echo "<b>Interest:</b>";
print_r ($person2->CalculateInterest());

【问题讨论】:

【参考方案1】:

正如您所说,$Balance 属性是私有的。私有意味着它不能从定义类外部访问,甚至不能从继承该属性的子类访问。

<?php
class Foo 
    private $bar="baz";
    public function __construct()echo $this->bar;
    public function getBar()return $this->bar;

class Bar extends Foo 
    public function __construct()echo $this->bar;

new Foo;
new Bar;

如果我们运行这段代码,我们会得到错误:

baz
PHP Notice:  Undefined property: Bar::$bar in php shell code on line 2

所以,你有两个选择。您可以改用您的吸气剂:

<?php
class Foo 
    private $bar="baz";
    public function __construct()echo $this->bar;
    public function getBar()return $this->bar;

class Bar extends Foo 
    public function __construct()echo $this->getBar();

new Foo;
new Bar;

或者,您可以将属性声明为protected 而不是private

<?php
class Foo 
    protected $bar="baz";
    public function __construct()echo $this->bar;
    public function getBar()return $this->bar;

class Bar extends Foo 
    public function __construct()echo $this->bar;

new Foo;
new Bar;

其中任何一个都给出了预期的输出:

baz
baz

有关属性和方法可见性的更多详细信息,请参阅documentation here。

【讨论】:

以上是关于无法查看从父类继承的属性 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

Python类 - 从父类继承属性到顶层

java子类从父类继承某个属性,怎么添加特定的注解不影响父类

Dart - 从父类方法访问继承的静态属性的方法

子类从父类继承过来的方法可以操作子类自己定义的成员变量吗

Java面向对象特征

简述在类的继承关系中,子类可以继承父类的都有哪些成员