首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法查看从父类继承的属性

无法查看从父类继承的属性
EN

Stack Overflow用户
提问于 2019-03-20 18:52:12
回答 1查看 75关注 0票数 0

我创建了一个包含以下变量的银行帐户类,$Balance变量是私有的,因此我使用setter和getter函数访问余额的私有属性。

代码语言:javascript
复制
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。

代码语言:javascript
复制
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(),但出现了以下错误:

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

代码语言:javascript
复制
//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());
EN

回答 1

Stack Overflow用户

发布于 2019-03-21 16:05:35

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

代码语言:javascript
复制
<?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;

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

代码语言:javascript
复制
baz
PHP Notice:  Undefined property: Bar::$bar in php shell code on line 2

所以你有两个选择。您可以使用您的getter代替:

代码语言:javascript
复制
<?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

代码语言:javascript
复制
<?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;

这两种方法中的任何一种都提供了预期的产出:

代码语言:javascript
复制
baz
baz

有关属性和方法可见性的详细信息,请参阅这里的文件

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55268188

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档