2. Anonymous class, anonymous class object, private/protected/public keyword, abstract abstract class, abstraction method, use of final keyword, multithreading thread class Start method principle

Source: Internet
Author: User
Tags thread class

Package com.bawei.multithread;//Note: Template methods we usually use abstract classes or abstract methods! Here we do not use abstract class/abstract method in order to facilitate use in this class Templatethread {//If this method does not want the quilt class or other people arbitrarily change [such a subclass can not overwrite the method], Here the method must be set to the final method public final void println (String message) {System.out.println ("#######################"); Wrapprint ( message); System.out.println ("#######################");} This method only allows the quilt class or itself to be covered, so here we use the protected keyword! protected void Wrapprint (String message) {}public static void main (string[] args) {//Here an object was created with the anonymous subclass of the Templatethread class, and covers the Wrapprint () method of this Templatethread class!  Templatethread T1 = new Templatethread () {@Overrideprotected void Wrapprint (String message) {//TODO auto-generated method StubSystem.out.println ("*" +message+ "*");}; T1.println ("haha");//Here is actually an anonymous subclass of the Templatethread class that creates an anonymous object and then calls the println () method of the anonymous object! Only here//with anonymous subclasses, overriding the Wrapprint method in the parent class, as well as reminding you that if the method in the parent class "Templatethread" is an abstract method, then the subclass must implement this abstract method, If this method in the parent class is just an empty method body, the subclass can choose to overwrite or//choose not to overwrite!! New Templatethread () {@Overrideprotected void Wrapprint (String message) {//TODO auto-generated method stubSystem.out.println ("=======" +message+ "=====");}. println ("haha");}}

I think our above code has been very good to explain our title to tell the knowledge point, here to learn, and then one is: This anonymous object created by anonymous class, and then call this anonymous object println () method is actually modeled after our Thead thread class start () Method implementation, that is to say: Our Multithreaded thread class start () method of the source code, you can see actually called the Start0 () method, and this method is native modified, that is, written by C + +, Inside this method, we actually call the run () method of our thread class, which is much like we call the Wrapprint () method in the println () method in the parent class Templatethread in the code above, and here the Wrapprint () The method is just an empty implementation, that is, subclasses can be implemented or can not be implemented, in fact, we call Thead to create a subclass is consistent, we can override the Thead class of the Run () method, yekeyi not overwrite, this is because thead/ The class itself has implemented the run () method for us, and if it has to be overridden or implemented by subclasses, this method can be set to an abstract method! Also in Templatethread we can set Wrapprint as an abstract method, then the subclass of Templatethread must implement this method, and then the object of the subclass can call the println () method directly! In terms of the thread class that Javaapi gives us, we can override its start () method, because this method is not final, but if we overwrite the start () method, then it will not have the Start0 () method in the source code to invoke C + + The program! So we're explaining why my name calls the run () method and we're calling the start () method when we run the program. The reason is that the Thead class here is a technique of template method, is similar to the code we wrote above, in a method 1 called another Method 2, we need to overwrite the method 2, but we run the code when the call is Method 1, because Method 1 for we call the Method 2, and other work was done before and after calling Method 2!

2. Anonymous class, anonymous class object, private/protected/public keyword, abstract abstract class, abstraction method, use of final keyword, multithreading thread class Start method principle

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.