PHP Class 继承私有 属性 构造

PHP Class Inheritance private property construct

我参加了第 3 年的 webDesign 模块,侧重于 OOP 概念,我正在努力掌握这些概念,但正在慢慢掌握...

一道作业题如下:

我对上面的问题进行了编码并且得到了正确的输出但是:

  1. 我不确定问题 A 和 B 中 greet() 方法的编码方式是否正确? 2.The constructorsub class 内的相关 properties 对于问题 B,我怀疑这些编码是否正确......尽管我得到了正确的输出,请查看代码中的注释
  2. 我没有看到扩展 class 的好处,也许我没有看到它是因为我用错了方法..?

感谢任何建议,代码如下。

A)

class Animal{
    private $name;

    public function __construct($dogName){
        $this->name = $dogName;
    }//construct

    public function greet(){
        $sting = "Hello I'm some sort of animal and my name is .$this->name.";
        return $sting;
    }//function
}//class

$animal = new Animal('Jock');
echo $animal->greet();

B - 子类

 class Dog extends Animal
    {
        private $animalType;

        public function __construct($animalType, $dogName){
            $this->$animalType = $animalType;
            $this->dogName = $dogName; //IS THIS LINE CORRECT, IF YES WHY SHOULD I USE IT AGAIN IN PARENT::_CONSTRUCT?

            //Call Animal Constructor To Finish
            parent::__construct($dogName);
        }//constructor

        public function greet($value1, $value2){
            $this->animalType = $value1;
            $this->dogName = $value2;
             $string = "Hello, I'm a ". $value1. " and my name is ". $value2;
            return $string;
        }
       }

$dog = new Dog('Dog', 'Jock');
echo $dog->greet('Dog', 'Jock');

我认为这个问题的措辞有点不正确。我认为它希望您创建属性 protected 而不是 private 来扩展 class (否则您将不得不在子 class 中重新定义相同的属性) .

如果是这种情况,这里是一个显示 class 继承的示例:

class Animal
{
    protected $name;

    public function __construct($name)
    {
        $this->name = $name;
    }

    public function greet()
    {
        return "Hello I'm some sort of animal and my name is {$this->name}.";
    }
}

$animal = new Animal('Jock');
echo $animal->greet();

class Dog extends Animal
{
    public function greet()
    {
        return "Hello I'm a dog and my name is {$this->name}.";
    }
}

$dog = new Dog('Jock');
echo $dog->greet();

你可以看到我们不需要重新定义 属性 $name 和构造函数。 class Dog 知道它是一只狗,所以我们不需要告诉它 - 我们只需相应地调整我们的 greet 方法。这显示了我们如何从 Dog 访问 protected 属性 $name,并显示了我们如何覆盖 Dog 中的 greet 方法。

我希望这对这个主题有所启发。

我认为您不需要 Dog class 中的构造函数。 greet 函数看起来不对。我认为您必须使用名称 getter (getName).

来使用 Animal class 中的名称
class Animal
{
    private $name;

    public function __construct($name)
    {
        $this->name = $name;
    }//construct

    public function greet()
    {
        $sting = "Hello I'm some sort of animal and my name is " . $this->getName();
        return $sting;
    }//function

    public function getName() {
        return $this->name;
    }
}//class

$animal = new Animal('Jock');
echo $animal->greet();

class Dog extends Animal
{
    public function greet()
    {
        $string = "Hello, I'm a dog and my name is " . $this->getName();
        return $string;
    }
}

$dog = new Dog('Jock');
echo $dog->greet();

您的代码有问题:

1) 为什么会有 Dog::$animalType 字段?扩展泛型 Animal class 的目的是使其与 Dog subclass 相当具体。相反,只需使用 strtolower(__CLASS__) 作为动物类型。

2)Dog::greet() 方法不需要任何参数。它可以清楚地使用$this->dogName

3) 您应该将两个名称字段命名为 Animal::$nameDog::$name。这样,子 class 将从其父 class 继承并覆盖值。相反,您引入了执行相同操作的附加字段,并打破了父子之间的逻辑关系 classes.

4) 父子 classes 都应该对同名的方法有相同的签名,这样继承才有意义。并且由于子 classes 照原样继承父 classes,因此无需重新定义它们(在您的情况下,构造函数)。

class Animal
{
    private $name; // can't be private (unless you use a getter), if you want to extend it

    public function __construct($name)
    {
        $this->name = $name;
    }

    public function greet()
    {
        return "Hello I'm some sort of animal and my name is " . $this->getName();
    }

    protected function getName()
    {
        return $this->name;
    }
}

class Dog extends Animal
{
    public function greet()
    {
        return "Hello, I'm a " . strtolower(__CLASS__) . " and my name is " . $this->getName();
    }
}

$animal = new Animal('Cow');
echo $animal->greet();

$dog = new Dog('Max');
echo $dog->greet();

另一种方法

<?php
class Animal{
   protected $name;

    public function __construct($dogName){
        $this->name = $dogName;
    }//construct

    public function greet(){
        $sting = "Hello I'm some sort of animal and my name is .$this->name.";
        return $sting;
    }//function
}//class

$animal = new Animal('Jock');
echo $animal->greet();

class Dog extends Animal
    {
           private $animalType;
            public function __construct($animalType,$dogname){
             $this->animalType = $animalType;
             parent::__construct($dogname);     
            }

        public function greet(){

            echo  "Hello, I'm a ".$this->animalType. " and my name is ".$this->name;

        }
       }

$dog = new Dog('Dog', 'New Dog');
echo $dog->greet();

?>