It's time to look at the living space objects and methods - and want to learn object-oriented,I do not know where to put the methods and objects is a very embarrassing thing - after all, we still have to face the garbage collector,Want to make the garbage collector to help you do more,We have to understand its working mechanism to some extent,Learn how and where to store objects,if not,You have to write a program to the vulnerable!
We have already mentioned the basic concept of the heap in front of the course which,Yes,Objects (instances) is placed on the heap - I put it vividly described as "garbage"。
Stack in the stack is a method
The title looks like a little description of the problem,But it is the image of the,Methods which have been placed on the stack,Which method comprises local variables。
That's why sometimes a local variable, also known as "stack variables."。
The method will be pushed onto the stack them one by one,The first to be called will be cut in the innermost,In the top of the stack is the last call,The method then finished will be ejected from the top of the stack - Let's take chestnuts:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
class Weapon { func fire() {} } class Gun:Weapon { var ammo = 0 override func fire() { print("bang!") } func reload() {} func reloadAndFire() { self.fire() self.reload() self.fire() } } var gun = Gun() gun.reloadAndFire() |
Or familiar weapons,When we call reloadAndFire() when,This method was pressed into the top of the stack - this time it actually stack them it was only a,It calls the fire () Technique,Then the fire is pressed into the top of the stack,The reloadAndFire() Because not yet finished,It was down to the bottom,Then fire () Finished the,It will be pop stack, reloadAndFire() Back to the original position,Then the system will continue to implement it,Then it calls the reload() Technique,Then another fire() ,Finally reloadAndFire() Finished,Also pop。
Examples of that statement in the method in it?
We said about the content of the referenced,Regardless create an instance anywhere,Examples are present in the heap,Local variables hand holding the remote control can only be。
Examples of variables in the
Speaking examples,It is followed instance variables inside live together on the heap,Their life cycle will grow a lot more than local variables - along with examples of life and death。so,We call an instance method in which variable,But the method is ejected instance variables will not go away,But wait until the instance is recovered will follow the example of Soul frog。
Create an object
This time we will be able to create an object of detailed description of the whole process:
- Declares reservoir,Mark it as an instance of a reference type;
- Create an object,It exists in the heap
- The reference heap objects connected with the reservoir - a reference to the address in the incoming object。
Original article written by LogStudio:R0uter's Blog » "Stack":In the end what happened "heap",What is a "stack"?
Reproduced Please keep the source and description link:https://www.logcg.com/archives/1133.html