如何创建一个只继承方法而不继承父类属性的子类?

假设我有这个代码:


首先我创建了父级


 class Country {

  public $country;

  public $populationcountry;

  public $language;     

使用一些方法(与此问题无关)


public function function1() {   }

public function function2() {    }

             .

             .

                }  //end of parent class

然后我创建了孩子


 Class city extends Country 

{

public $populationcity;

然后我创建对象(对于这个例子,我只创建了一个)


$city1 = new city();


$city1->populationcity = 10000; 

和一组对象


$cities = [$city1];    

最后我只想“回显”孩子的属性(人口城市)


foreach ($cities as $city) {

foreach ($city as $k => $v) {

$city->populationcity;

echo $k . ': ' . $v . '<br>';

 }

}   

输出:

人口城市:10000

国家:

人口国家:

语言:


我想保留父级的方法,而不是父级的属性。我怎样才能做到这一点?


David 在评论中告诉我将属性设置为 Private。我这样做了,并且工作正常,但是当我创建 Country 对象时,它会在子类中打印父类的属性。


这是代码,当父属性为 Public 时,它为我提供此输出。


人口城市:10000

国家:

人口国家:

语言:

国家:英格兰

人口国家:30000

语言:英语


它应该打印:


人口城市:10000

国家:英格兰

人口国家:30000

语言:英语


当我将它们设置为 Private 时,我得到了这个:


致命错误:未捕获错误:无法访问 C:\xampp\htdocs\ejercicios.php:141 中的私有属性 Country::$language 堆栈跟踪:#0 {main} 抛出在 C:\xampp\htdocs\ejercicios.php 中141


当我将它们设置为 Protected 时,我得到了这个:


致命错误:未捕获的错误:无法访问 C:\xampp\htdocs\ejercicios.php:141 中的受保护属性 Country::$language 堆栈跟踪:#0 {main} 抛出在 C:\xampp\htdocs\ejercicios.php 141


米琪卡哇伊
浏览 195回答 2
2回答

温温酱

看起来像一个糟糕的设计模式。对我来说,一个国家可以包含几个不同的城市。因此,城市不是国家。你的国家实体类不应该是这样的吗?class Country{&nbsp; &nbsp; /**&nbsp; &nbsp; &nbsp;* Collection of City objects&nbsp; &nbsp; &nbsp;* @var ArrayObject&nbsp; &nbsp; &nbsp;*/&nbsp; &nbsp; protected $cities;&nbsp; &nbsp; protected $name;&nbsp; &nbsp; protected $population;&nbsp; &nbsp; protected $language;&nbsp; &nbsp; public function setCity(City $city) : self&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if ($this->cities == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->cities = new ArrayObject();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $this->cities->append($city);&nbsp; &nbsp; &nbsp; &nbsp; return $this;&nbsp; &nbsp; }&nbsp; &nbsp; public function getCities() : ArrayObject&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if ($this->cities == null) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $this->cities = new ArrayObject();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $this->cities;&nbsp; &nbsp; }}无论如何......让我们用php自己的反射类来解决您的问题。要获取声明类的属性,只需在城市类中实现以下功能即可。class City extends Country{&nbsp; &nbsp; public $populationCity;&nbsp; &nbsp; public function getData() : array&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; $data = [];&nbsp; &nbsp; &nbsp; &nbsp; $reflection = new ReflectionClass($this);&nbsp; &nbsp; &nbsp; &nbsp; $properties = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);&nbsp; &nbsp; &nbsp; &nbsp; foreach ($properties as $property) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ($property->getDeclaringClass() == $reflection) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $data[$property->getName()] = $property->getValue($this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return $data;&nbsp; &nbsp; }}有了这个,您只能获取City类的属性。让我们试一试...$city = new City();$city->populationCity = 10000;var_dump($city->getData());希望这可以帮助。
打开App,查看更多内容
随时随地看视频慕课网APP