search

Home  >  Q&A  >  body text

Inheritance problem and solution

This example first does not instantiate the parent class, and then when the subclass does not override the method of the parent class, $cat1->cry() calls the parent class. What does it mean? The subclass method is also written as echo 'animal call.. ';? What I understand is that if this is the case, doesn’t saying it mean that you haven’t said it?

3433333.png


小周小周2325 days ago1035

reply all(3)I'll reply

  • Summer

    Summer2018-07-19 11:45:51

    Your cat class inherits the animal class. If there is no cry method in the cat class, your $cat1->cry() calls the cry method of the parent class and outputs the animal cry. . When your cat class has a cry method, it is called the parent class override. Then $cat1->cry() gives priority to calling the cry method of the subclass and outputs abc

    reply
    0
  • 无忌哥哥

    无忌哥哥2018-07-19 11:12:39

    The subclass does not override the parent class, that is, the subclass just inherits the parent class without making changes. For example, the parent class $i=1; the subclass does not define $i, then the parent class is called in the subclass. $i is still 1. If the subclass covers the parent class, for example, $i=2 is defined in the subclass; then $i is equal to 2 in the end.

    reply
    0
  • 小周

    Um, okay, but the coverage of the method I asked about also means this, right? Prove that I understand correctly?

    小周 · 2018-07-19 11:17:09
  • Cancelreply