Java about anonymous inner classes

Source: Internet
Author: User

1, in a nutshell, a method in the TThread class (thread Class) Thread.CurrentThread () refers to getting the currently running thread object, for example: Thread.CurrentThread (). GetName () is to get the name of the currently running thread. It's almost probably incomplete. Who made me a novice? Thread T=thread.currentthread (). GetName (), T.getname. Memorize this writing format. It is very helpful to study in the future. 2, if an anonymous class implements an interface, its principal must implement the method of the interface. (The following example is Super Classic) 1.interface pr   2. {   3.  void Print1 ();   4.}    5.  6.public class nonameclass    7. {   8.   Public PR dest ()    9.   {   10.        return new PR () {   11.       public void Print1 ()    12.        {   13.         System.out.println ("Hello world!!");    14.      }   15.   };   16.}    17.  18.public static void Main (String args[])    19. {   20.    nonameclass C =  New Nonameclass ();   21.    PR hw=c.dest ();   22.     hw.print1 ();   23.}    24.}

2 package COM.L;

public class DemoXiancheng2 {

public static Demointerfence A () {return new demointerfence () {@Override public void print () {//TODO Au         to-generated method Stub System.out.println ("This is a niming neibu lei");  }   }; } public static void Main (string[] args) {//TODO auto-generated method stub//demoxiancheng2 de=new DemoXiancheng2 (   );   Demointerfence demoi=demoxiancheng2.a (); Demoi.print ();

}

} Summary: New creates a Demointreence object, and later {...} The action in the default action on this object. Please take shorthand, anonymous (parent class, abstract class, interface these things, which need to remember, see Don't panic) * An abstract class is not directly new, must have an implementation class before we can new out of its implementation class. The pseudo-code (method) above   means that new is an implementation class of Father (pumped), which is an anonymous inner class. Note:    anonymous Inner classes cannot have a constructor method.     Anonymous inner classes cannot define any static members, methods, and classes.     Anonymous inner class cannot be public,protected,private,static.     can only create an instance of an anonymous inner class.    an anonymous inner class must be behind new, using its implicit implementation of an interface or implementation of a class.     because the anonymous inner class is a local inner class, all the restrictions on the local inner class are applied to it.    Internal classes can only access static variables or static methods of external classes.

Function: 1, simplify the wording, save a middle class 2, there is a class child, it both want to inherit the parent's count () method, and want to implement the Icount interface count method, this time what to do?          The inner class can do the same: class Child extends Parent {ICount GetCount () {return new ICount {int i = 0;          int count () {return (I *= 2); }        }      }    }

Java about anonymous inner classes

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.