First consider, people a; how this code will be implemented in the compiler? It must be definitely first bound to object A first, and must be assigned a space for object a. At the same time, we create a subclass of people due to Man Is a people. According to the principle of polymorphism and replacement, we can of course allow object A to save a value of a MAN type (this is the performance of the replacement principle). This is an intuitive description, but there are some difficulties in the process of programming language. We know that inheritance is an extension interface and implementation, then we will hardly guarantee that the MAN type does not extends to the people type, and once we make an extension, how can we use the space to store the people's object value? ?
People a;
Man b = new man ();
A = B;
Such a code will first cut the B object, and then store it to the A object space. However, this is not what we expect. Then, in order to support the inheritance, polymorphism, and replacement principles of OOP, it is necessary to avoid the occurrence of object cuts. What all the allocation space models we will use? Commonly used three ways:
1, only for the storage space of the base class PEOPLE, does not support object polymorphism and replacement principles. Such model memory is compact, and storage efficiency is high.
2. Allocate the maximum class object in the inheritance tree (here is the object value space of the Man class), such a model is simple, and the multi-state and replacement principle can avoid object cutting problems, but very wasteful memory space is very wasteful. obvious.
3. Only allocate the storage space required to save a pointer, and assign the space size required to assign the object actual type by the heap at runtime, which can also achieve the multi-state and replacement principles to avoid object cutting problems. (That is to say A is just a reference to the M object, not the real object, the generation of the real object must rely on programmer's explicit statement).
For the three memory models mentioned above, 1 and 3 are employed by some programming languages. I believe that everyone should start to understand. Yes, C as a successor of C language, forcing the pursuit of efficiency to force it to use the first minimum static spatial assignment, due to the operational efficiency based on the stack space is much higher than the operational efficiency based on the stack-based space, So C allows the object to be saved with the stack space, but also allows the heap space to save objects, which can be said that C is a memory model that mixed with 1 and 3, and C is based on 1 memory model object, that is, the stack The object of memory space is no way to reflect the principle of polymorphism and replacement (please think about what object in C is stack), and 3-memory model-based object will support polymorphism and replacement principles (I want to think about C What object is based on a heap). Here, we can finally open the first layer of fog, many people know that only pointers and references in C can support the polymorphic behavior of the object, but why? The best explanation is made.
Java language has a great difference due to design concepts and C , which uses the third object model, all objects (except for basic type objects) are all based on heap assignments. This is also the reason why the Java language must adopt a virtual machine. A large part of the object in C is not required to manage (static spatial objects), and in Java, if the virtual machine mechanism is not used, all objects need programmers to manage, and such development cost will be huge. Not realistic. This will uncover the second layer of fog. When we always fight for the virtual machine when comparing C and Java language, we will fight for its own value, but when you leave the so-called good and bad discussion, enter When the inherent object of its language itself stores in nature, there may be a clear understanding of your own sound. Let us continue to go, different object memory allocation models directly affect the meaning of the assignment of its programming language. In various programming languages, the assignment can give two different semantic explanations: copy semantics and pointer semantics. Obviously, since C supports two-phase hybrid object storage model (but the default storage method is a stack storage), the default assignment semantic in C is the former, but C provides a functional support for pointer semantics (in copy The constructor is customized in the = operator overload. The latter is used in Java. This is also the last fog we unveiled, and different object storage models directly lead to different assignments.