从实例中获取从父 class 到子 class 的值
Getting values from parent class to child class from an instance
我有一个人 class $name
属性 和一个官员 class $officer_name
属性。我需要能够从 class 人那里得到 $name
属性 给官员 class。在这种情况下,它应该有 "Major Blake" 的输出。我是否需要创建官员 class 的实例才能回应这一点?
class person {
protected $name;
private function set_name($fv_name) {
$this->name = $fv_name;
}
public function get_name() {
return $this->name;
}
function __construct($fv_name) {
$this->set_name($fv_name);
}
}
class officer extends person {
private function give_rank(){
return "Major ";
}
function __construct() {
echo $officer_name = $this->give_rank() . parent::get_name();
}
}
$iv_person = new person("Blake");
请尝试此代码:
你的 class :
<?php
class person {
protected $name;
private function set_name($fv_name) {
$this->name = $fv_name;
}
public function get_name() {
return $this->name;
}
function __construct($fv_name) {
$this->set_name($fv_name);
}
}
class officer extends person {
private function give_rank(){
return "Major ";
}
function __construct($fv_name) {
parent::__construct($fv_name);
}
function output(){
return $this->give_rank().parent::get_name();
}
}
你的电话:
$iv_person = new officer("Blake");
echo $iv_person->output();
我有一个人 class $name
属性 和一个官员 class $officer_name
属性。我需要能够从 class 人那里得到 $name
属性 给官员 class。在这种情况下,它应该有 "Major Blake" 的输出。我是否需要创建官员 class 的实例才能回应这一点?
class person {
protected $name;
private function set_name($fv_name) {
$this->name = $fv_name;
}
public function get_name() {
return $this->name;
}
function __construct($fv_name) {
$this->set_name($fv_name);
}
}
class officer extends person {
private function give_rank(){
return "Major ";
}
function __construct() {
echo $officer_name = $this->give_rank() . parent::get_name();
}
}
$iv_person = new person("Blake");
请尝试此代码: 你的 class :
<?php
class person {
protected $name;
private function set_name($fv_name) {
$this->name = $fv_name;
}
public function get_name() {
return $this->name;
}
function __construct($fv_name) {
$this->set_name($fv_name);
}
}
class officer extends person {
private function give_rank(){
return "Major ";
}
function __construct($fv_name) {
parent::__construct($fv_name);
}
function output(){
return $this->give_rank().parent::get_name();
}
}
你的电话:
$iv_person = new officer("Blake");
echo $iv_person->output();