php - Why does subclass overriding parent class properties and methods not override parent class properties and methods?
PHP中文网
PHP中文网 2017-05-16 13:11:34
0
8
1092

1. Since inheritance is passed by reference, why does the subclass override the properties and methods of the parent class not change the properties and methods of the parent class. What does inherited reference transfer mean? Under what circumstances will the member methods and member properties of the parent class be changed?

2. Since static properties, methods and constants are in the static area, why are the original properties and variables still not overwritten when rewritten in subclasses?

3. The newly edited question - about the static area - everyone knows that there are only member variables in the object object, only the addresses of member methods in the heap, member methods and classes are in the code area, and static members Methods are different from member methods. Static member methods are in the static area. obj->static member variables will report an error, while obj->static member methods can be called successfully. Why is this? Can anyone tell me about the relationship between code area, static area and objects? Thank you very much! ! ! ! !

PHP中文网
PHP中文网

认证0级讲师

reply all(8)
滿天的星座

None of the answers above are to the point. Baidu by yourself: late binding and early binding; also take a look at tij’s knowledge about inheritance and polymorphism

漂亮男人

You can try to understand it this way, inheritance is passed by reference, the subclass is the inherited parent class, and the properties and methods of the parent class, then all the properties and methods of the parent class must be in the memory.
However, when you override the method of the parent class in the subclass, it is actually equivalent to moving the pointer from the a() method of the parent class to the overridden a() of the subclass. In fact, the a() of the parent class still exists, not It will actually replace the a() method of the parent class from the memory.

Inherited reference passing: means that there is only one copy of the parent class's code in the memory. For example, the memory address is 0x000010. When the subclass inherits, the parent class's code is read directly from this memory address. Instead of re-copying the code into memory.

仅有的幸福

You can take a look at the dynamic binding mechanism. The subclass and the parent class will form two separate method tables and respective data types in the method area. . . Will not cover

漂亮男人

Personal understanding of inheritance:
The methods that the parent class already has, the subclasses that inherit the parent class have them by default (methods and attributes). However, subclasses can choose to override the methods of the parent class. In this case, when the subclass calls these overridden methods, it will give priority to calling the method modified by the subclass. If the subclass does not modify the method, , the method of the parent class will be called by default.
As for what the original poster said, inheritance is pass-by-reference. I personally think that when inheriting, these methods and attributes point to the parent class by default. However, when actually calling, it will detect whether the subclass has overridden the methods or attributes of the parent class. If If there is, the actual call will point to the methods and properties overridden by the subclass. If not, it will still point to the methods and properties of the parent class.
Personal humble opinion...

迷茫

A subclass is an inherited parent class. The subclass can inherit the attributes and methods of the parent class, but it cannot change the parent class. The subclass has only one parent class, but the parent class can have many subclasses. If the subclass changes the parent Do the attributes and methods of the class itself need to inherit new methods and attributes again? There is no solution. . .

小葫芦

A subclass can only override the parent class. The code first executes the method in the subclass and no longer continues to execute the overridden method of the parent class.

PHPzhong

Inheritance in the PHP language can only be inherited from high to low and cannot be overwritten in reverse

给我你的怀抱

I inherit the methods of the parent class, which is equivalent to writing all the methods of the parent class into my class. As for the methods that conflict with my existing ones, I just don’t write them in. That is to say, if the parent class has it and does not have it, then it will be inherited. If the parent class has it, you also have it, so it will be based on you.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template