php - 为什么子类重写父类属性和方法不会覆盖父类的属性和方法?
PHP中文网
PHP中文网 2017-05-16 13:11:34
0
8
1081

1.既然继承是通过引用传递 那为什么子类重写父类的属性和方法不会改变父类的属性和方法呢。 那继承的引用传递指的是什么 什么情况下才会改变父类的成员方法和成员属性。

2.既然静态属性和方法和常量 在静态区 那在子类重写 为什么依然不会覆盖原来的属性和变量。

3.新编辑的问题— — — 就是关于静态区—— 大家都知道查看对象 对象里只有成员变量堆里只有成员方法的地址 成员方法和类是在代码区的 而静态成员方法和成员方法不一样 静态成员方法是在静态区 obj->静态成员变量 会报错 而obj->静态成员方法 可以成功调用。这是为什么 有人能讲一下 代码区 和静态区和对象的关系么 感激不尽!!!!!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!