PHP类继承私有属性构造

时间:2016-10-20 08:45:38

标签: php class oop subclass

我在webDesign上学习了第3年的模块,重点关注OOP概念,这些概念我很难掌握,但却慢慢到达那里......

作业分类问题如下:

enter image description here

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

  1. 我不确定我对问题A&中greet()方法的编码方式B是对的吗? 2.对于问题B constructor内的properties和相关的sub class,我怀疑这些是否正确编码...即使我得到正确的输出,请参阅注释在代码中
  2. 我没有看到扩展课程的好处,也许我没有看到它,因为我错误地解决了这个问题。?
  3. 任何建议表示赞赏,代码如下。

    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 - SubClass

     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');
    

4 个答案:

答案 0 :(得分:1)

我认为问题的措辞略有错误。我认为它希望你创建属性protected而不是private来扩展类(否则你必须在子类中重新定义相同的属性)。

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

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和构造函数。班级Dog知道它是一只狗,所以我们不需要告诉它 - 我们只是相应地调整我们的问候方法。这显示了我们如何从protected访问$name属性Dog,并展示了我们如何覆盖greet中的Dog方法。

我希望这能说明这个问题。

答案 1 :(得分:1)

我认为你不需要Dog类中的构造函数。问候功能看起来不对。我认为您必须使用名称getter(getName)来使用Animal类中的名称。

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();

答案 2 :(得分:1)

代码问题:

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

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

3)您应将两个名称字段命名为Animal::$nameDog::$name。这样,子类将从其父类继承并覆盖值。相反,你引入了另外的字段来做同样的事情,并打破了父类和子类之间的逻辑关系。

4)父类和子类对于具有相同名称的方法应该具有相同的签名,以便继承可以有目的。由于子类按原样继承父类,因此无需重新定义它们(在您的情况下,构造函数)。

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();

答案 3 :(得分:0)

另一种方法

<?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();

?>
相关问题