Author: qlampskyface
Contact your author: xiaozuidaizhi@sina.com
It is well known that the two uses of this are very common.
1. Replace the current instance. For example, this. Member variable name, this. Member method name
2. Instead of constructor, such as this ().
The above two points will not be described again.
However, I think this is still a phenomenon that everyone may not notice:
Maybe sometimes you will find the following phenomenon:
Public class test implements runnable {
Thread thread1;
Thread thread2;
Public test () {
Thread1 = New Thread (this, "1"); // ----------------------> |
Thread2 = New Thread (this, "2"); // ----------------------> |
}
Public static void main (string args []) {
Test T = New Test ();
T.startThreads ();
}
Public void run () {
// do thread's thing
}
Public void startthreads () {
Thread1.start ();
Thread2.start ();
}
}
At |, you may be strange: Why can you call yourself when you haven't created it?
In order to explain the phenomenon above, the inheritance of the constructor must be explained first.
If there are two classes into inheritance, then
If the default constructor does not explicitly declares, then the sub-constructor must first call its parent class default constructor (in fact, I think the first constructor is more appropriate, that is, the constructor without income, then call yourself Constructor.
Below we will follow the strange phenomena, why can I use this to replace itself without creating a subclass object? That is because the default constructor of the parent class has been called before the subclass constructor is called. The parent class is Object. You can also let Test inherit a class to see if its parent class constructor is called.
In the above, since the own capacity is limited, it does not discuss this topic, but it is only the skin to explain, I hope to be able to throw the jade. Please correct!
Correct: Because I am too shallow, the explanation of the above phenomenon has had fallacy, and I am sincere apologize to everyone. Thanks to the Dreamhead brother to see this embarrassment, it is a valuable opinion, in order to go to the fake, here, I added the true cause of the above phenomenon as follows:
People from C / C may have a deep understanding of this process. Take C as an example, usually our dynamic allocation work is completed.
1 Call Malloc Assign a memory space, but this memory is just a memory required to have an object and is not initialized.
2 Call the corresponding initialization method to initialize this memory. In C , the New is actually the two methods. When we are usually used, you only see New in allocating memory, in fact, it also calls the constructor (equivalent to the initialization method) completes the initialization.
Java's process is also the same. In addition to the basic type in Java, we hold the reference (featured a pointer in the C language, but not pointers!). The structure of the object is divided into two steps assigned a memory and call constructor. When the program is executed to the constructor, the actions that allocate memory have been completed. In the above program, the Thread constructor is incorporated into a reference, which is an address, and at this time, because Test has allocated memory, it is of course OK as a parameter. As for the problem that I said, I didn't say anything wrong, but I didn't work with this problem. Please pay attention. Thank you again Dreamhead brother.