在PHP中的多级继承中重载一个不';我不接受任何论据
我正在学习PHP,刚从中级开始。下面是类层次结构-在PHP中的多级继承中重载一个不';我不接受任何论据,php,Php,我正在学习PHP,刚从中级开始。下面是类层次结构- FamilyTree | | Grandparents | | Parents 父母有一个名为$dob的额外属性,其余的继承自祖父母 我能够重载set_values(),因为它们的参数数量不同,既然它不接受任何参数,我如何重载display_values() 代码如下: <?php class FamilyTree { protected $name; protected $se
FamilyTree
|
|
Grandparents
|
|
Parents
父母
有一个名为$dob
的额外属性,其余的继承自祖父母
我能够重载set_values()
,因为它们的参数数量不同,既然它不接受任何参数,我如何重载display_values()
代码如下:
<?php
class FamilyTree {
protected $name;
protected $sex;
protected $married;
protected $number_of_kids;
public function display_values() {
echo $this->name . "<br />";
echo $this->sex . "<br />";
echo $this->married . "<br />";
echo $this->number_of_kids . "<br />";
}
}
class Grandparents extends FamilyTree {
public function set_values($name, $sex, $married, $number_of_kids) {
$this->name = $name;
$this->sex = $sex;
$this->married = $married;
$this->number_of_kids = $number_of_kids;
}
}
class Parents extends Grandparents {
private $dob;
public function __call($function_name, $arguments) {
if($function_name == 'set_values' && count($arguments) == 5) {
$this->$name = $name;
$this->sex = $sex;
$this->married = $married;
$this->number_of_kids = $number_of_kids;
$this->dob = $dob;
}
}
}
$gp = new Grandparents();
$gp->set_values("James", "Male", 'yes', 5);
$p = new Parents();
$p->set_values("Samuel", "Male", 'yes', 2, '2/5/1974');
$gp->display_values();
方法重载的签名和方法名称必须相同。如果没有这两个函数,它就不会被认为是方法重载。在本例中,is不是重载,而是一个单独的方法。您只需通过此显示值($name、$sex、$marted、$numberofchilds)来重载,并为方法重载设置所有值,签名和方法名称必须相同。如果没有这两个函数,它就不会被认为是方法重载。在本例中,is不是重载,而是一个单独的方法。您只需通过此显示值($name、$sex、$marted、$numberofchilds)来重载,并设置所有值很抱歉让您失望,但您未能“重载”该方法 如果仔细看一下您的实现,如果调用
public function __call($function_name, $arguments) {
if($function_name == 'set_values' && count($arguments) == 5) {
$this->$name = $name;
$this->sex = $sex;
$this->married = $married;
$this->number_of_kids = $number_of_kids;
$this->dob = $dob;
}
}
你会注意到
应该是$this->$name
$this->name
、$name
、$sex
、$marted
和$number\u of_children
均未定义,应改为$dob
至$arguments[0]
$arguments[4]
\u调用
实现根本不会被调用,因为
相反,您可以做的是重写该函数,并使用parent::
调用父实现:
public function set_values($name, $sex, $married, $number_of_kids, $dob = '') {
parent::set_values($name, $sex, $married, $number_of_kids);
$this->dob = $dob;
}
我只是将$dob
初始化为一个空字符串,否则它将默认为NULL
。作为旁注,如果您想提供一个新的实现,它比原始实现使用更少的参数,您也可以使用默认参数:
public function set_values($name, $sex, $married, $number_of_kids = 0) {
parent::set_values($name, $sex, $married, $number_of_kids);
}
然后可以使用3个或4个参数调用
对于更复杂的组合,请使用每个参数的默认参数声明新实现(以使其与父实现兼容),并使用来处理或重定向调用
不过,重写显示值很简单,因为参数的数量相同:
public function display_values() {
parent::display_values();
echo $this->dob . "<br />";
}
公共功能显示\u值(){
父项::显示_值();
echo$this->dob.“
”;
}
总之,您的代码如下所示:
<?php
class FamilyTree {
protected $name;
protected $sex;
protected $married;
protected $number_of_kids;
public function display_values() {
echo $this->name . "<br />";
echo $this->sex . "<br />";
echo $this->married . "<br />";
echo $this->number_of_kids . "<br />";
}
}
class Grandparents extends FamilyTree {
public function set_values($name, $sex, $married, $number_of_kids) {
$this->name = $name;
$this->sex = $sex;
$this->married = $married;
$this->number_of_kids = $number_of_kids;
}
}
class Parents extends Grandparents {
private $dob;
public function set_values($name, $sex, $married, $number_of_kids, $dob = '') {
parent::set_values($name, $sex, $married, $number_of_kids);
$this->dob = $dob;
}
public function display_values() {
parent::display_values();
echo $this->dob . "<br />";
}
}
$gp = new Grandparents();
$gp->set_values("James", "Male", 'yes', 5);
$p = new Parents();
$p->set_values("Samuel", "Male", 'yes', 2, '2/5/1974');
$gp->display_values();
$p->display_values();
很抱歉让您失望,但您未能“重载”该方法
如果仔细看一下您的实现,如果调用
public function __call($function_name, $arguments) {
if($function_name == 'set_values' && count($arguments) == 5) {
$this->$name = $name;
$this->sex = $sex;
$this->married = $married;
$this->number_of_kids = $number_of_kids;
$this->dob = $dob;
}
}
你会注意到
$this->$name
应该是$this->name
$name
、$sex
、$marted
、$number\u of_children
和$dob
均未定义,应改为$arguments[0]
至$arguments[4]
但是您的\u调用
实现根本不会被调用,因为
相反,您可以做的是重写该函数,并使用parent::
调用父实现:
public function set_values($name, $sex, $married, $number_of_kids, $dob = '') {
parent::set_values($name, $sex, $married, $number_of_kids);
$this->dob = $dob;
}
我只是将$dob
初始化为一个空字符串,否则它将默认为NULL
。
作为旁注,如果您想提供一个新的实现,它比原始实现使用更少的参数,您也可以使用默认参数:
public function set_values($name, $sex, $married, $number_of_kids = 0) {
parent::set_values($name, $sex, $married, $number_of_kids);
}
然后可以使用3个或4个参数调用
对于更复杂的组合,请使用每个参数的默认参数声明新实现(以使其与父实现兼容),并使用来处理或重定向调用
不过,重写显示值很简单,因为参数的数量相同:
public function display_values() {
parent::display_values();
echo $this->dob . "<br />";
}
公共功能显示\u值(){
父项::显示_值();
echo$this->dob.“
”;
}
总之,您的代码如下所示:
<?php
class FamilyTree {
protected $name;
protected $sex;
protected $married;
protected $number_of_kids;
public function display_values() {
echo $this->name . "<br />";
echo $this->sex . "<br />";
echo $this->married . "<br />";
echo $this->number_of_kids . "<br />";
}
}
class Grandparents extends FamilyTree {
public function set_values($name, $sex, $married, $number_of_kids) {
$this->name = $name;
$this->sex = $sex;
$this->married = $married;
$this->number_of_kids = $number_of_kids;
}
}
class Parents extends Grandparents {
private $dob;
public function set_values($name, $sex, $married, $number_of_kids, $dob = '') {
parent::set_values($name, $sex, $married, $number_of_kids);
$this->dob = $dob;
}
public function display_values() {
parent::display_values();
echo $this->dob . "<br />";
}
}
$gp = new Grandparents();
$gp->set_values("James", "Male", 'yes', 5);
$p = new Parents();
$p->set_values("Samuel", "Male", 'yes', 2, '2/5/1974');
$gp->display_values();
$p->display_values();
除非我误解了您的意思,否则在这种情况下,您不会通过类parent中的u调用来过度加载set_值。若要重载,该方法必须不可访问
class Parents extends Grandparents {
private $dob;
public function __call($function_name, $arguments) {
echo 'called';
if($function_name == 'set_values' && count($arguments) == 5) {
$this->$name = $name;
$this->sex = $sex;
$this->married = $married;
$this->number_of_kids = $number_of_kids;
$this->dob = $dob;
}
}
}
这将不会打印$p的“调用”->设置_值(“Samuel”、“Male”、“yes”、“2”、“2/5/1974”)
要重写该方法,父方法必须是不可访问的,参数的数量无关紧要,请参见以下示例:
<?php
class FamilyTree {
protected $name;
protected $sex;
protected $married;
protected $number_of_kids;
private function display_values() {
echo $this->name . "<br />";
echo $this->sex . "<br />";
echo $this->married . "<br />";
echo $this->number_of_kids . "<br />";
}
}
class Grandparents extends FamilyTree {
private function set_values($name, $sex, $married, $number_of_kids) {
$this->name = $name;
$this->sex = $sex;
$this->married = $married;
$this->number_of_kids = $number_of_kids;
}
private function drawMe($a)
{}
}
class Parents extends Grandparents {
private $dob;
public function __call($function_name, $arguments) {
echo 'Args: ' . count($arguments);
echo ' | Function name: ' . $function_name;
if($function_name == 'set_values' && count($arguments) == 5) {
$this->$name = $name;
$this->sex = $sex;
$this->married = $married;
$this->number_of_kids = $number_of_kids;
$this->dob = $dob;
}
}
}
$p = new Parents();
$p->display_values();
echo '<br><br>';
$p->set_values("Samuel", "Male", 'yes', 2, '2/5/1974');
除非我误解了您的意思,否则在这种情况下,您不会通过类parent中的u调用来过度加载set_值。若要重载,该方法必须不可访问
class Parents extends Grandparents {
private $dob;
public function __call($function_name, $arguments) {
echo 'called';
if($function_name == 'set_values' && count($arguments) == 5) {
$this->$name = $name;
$this->sex = $sex;
$this->married = $married;
$this->number_of_kids = $number_of_kids;
$this->dob = $dob;
}
}
}
这将不会打印$p的“调用”->设置_值(“Samuel”、“Male”、“yes”、“2”、“2/5/1974”)
要重写该方法,父方法必须是不可访问的,参数的数量无关紧要,请参见以下示例:
<?php
class FamilyTree {
protected $name;
protected $sex;
protected $married;
protected $number_of_kids;
private function display_values() {
echo $this->name . "<br />";
echo $this->sex . "<br />";
echo $this->married . "<br />";
echo $this->number_of_kids . "<br />";
}
}
class Grandparents extends FamilyTree {
private function set_values($name, $sex, $married, $number_of_kids) {
$this->name = $name;
$this->sex = $sex;
$this->married = $married;
$this->number_of_kids = $number_of_kids;
}
private function drawMe($a)
{}
}
class Parents extends Grandparents {
private $dob;
public function __call($function_name, $arguments) {
echo 'Args: ' . count($arguments);
echo ' | Function name: ' . $function_name;
if($function_name == 'set_values' && count($arguments) == 5) {
$this->$name = $name;
$this->sex = $sex;
$this->married = $married;
$this->number_of_kids = $number_of_kids;
$this->dob = $dob;
}
}
}
$p = new Parents();
$p->display_values();
echo '<br><br>';
$p->set_values("Samuel", "Male", 'yes', 2, '2/5/1974');
它不接受我提到的任何参数。它不接受我提到的任何参数。那么,如果你只是将公共函数显示\u值()
添加到父类中会发生什么呢?@u\u mulder,如果我这样做,那么它就不会为$this->dob打印任何内容。那么,如果你只是添加公共函数显示\u值(),会发生什么呢
toParents
class?@u\u mulder,如果我这样做,那么$this->dob就不会打印任何东西