Call the anonymous inner class

The reason why is anonymous anonymous objects, because objects created directly, but did not assign a value to this object, it is called anonymous.

class NoObject 
{ 
 
    void Method () 
    { 
 
        System.out.println ( "NoNameObj the Hello" ); 
 
    } 
 
 
} 
 
class NoName A 
{ 
 
    public  static  void main (String [] args) { 
         
        new new NoObject () Method ();. // created here End after the object, and it did not directly assign a value to an object, but an object created after the completion of a direct call to the object, then the object can not be other objects called <br>                        // because to call an object must know his name it, how to call no name, this is the concept of anonymity. After the bin into a garbage can not be on call. 
 
    } 
 
}

Call the usual inner class

class Outer
{

    int num = 10;

    class Inner
    {

        void method()
        {

            System.out.println("The Outer Num is "+num);

        }

    }

    void sayInner()
    {

        new Inner().method();

    }

}


class InnerClassDemo2
{

    public static void main(String[] args) {
        
        new Outer().sayInner();

    }

}

The use of anonymous objects conditions:

Inner class must inherit or implement an external interface that meet this condition that we can use the inner class. Code Example:

abstract  class AbsDemo 
{ 

    abstract  void Demo (); 

} 

class Outer 
{ 

    int NUM = 10 ; 


    void sayInner () 
    { 
        / ** 
        * We directly place a new parent class or interface, but there is one thing to note that after the interface over new parent class or implemented, 
        * the back to add braces, plus the significance of braces is that said this is a class and subclass of the class or interface is behind the new keywords or achieve 
        * then we directly cover a class or method or implemented after the braces interface 
        * / 
        new new AbsDemo () 
        { 

            void Demo () 
            { 

                System.out.println ( "iS a NoNameInnerClass This Demo" ); 

            } 

        } .demo (); //The place is very error-prone, often our new abs () {} after you forget to call, because we just simply new abs () {} like this is not
                  // make any sense, why because this is an anonymous class , that no class name, gone, no longer anonymous quotes after you define. Can only be called immediately
                  // meaningful after this method will become. Another point is that after the anonymous call to complete the class definition, do not forget the semicolon at the end of the sentence, because after the new is
                  // a statement, and not the class is declared. 

    } 

} 


Class InnerClassDemo2 
{ 

    public  static  void main (String [] args) { 
        
        new new . Outer () sayInner (); 

    } 

}

Popular anonymous inner classes is this: is an anonymous subclass object. The method is defined by: new new parent class or interface () {} subclass content

Anonymous inner classes of applications:

A scenario: When the function parameter is the interface type, and the interface method is not more than three, then we can use anonymous inner classes be passed as parameters. such as:

interface InterfaceDemo
{

    public void show1();
    public void show2();

}

class InnerClassDemo2
{

    public static void main(String[] args) {
        
        method(new InterfaceDemo(){

            public void show1()
            {

                System.out.println("Show 1");

            }

            public void show2()
            {

                System.out.println("Show 2");

            }

        });

    }

    static void method(InterfaceDemo demo)
    {

        demo.show1();
        demo.show2();
    }

}

Here method () method requires an object interface type of the incoming order period is simple, we directly use the anonymous inner class object has been passed, there is also a place to note is that if this is the case now:

class InnerClassDemo2
{

    class Inner
    {


    }

    public static void main(String[] args) {
        
        new Inner();

    }


}

main function is static, he can not call static context among non-static member, then class Inner {} In this class which is equivalent to a member, but non-static naturally will complain.

Here there is a problem such as polymorphic Note:

interface InterfaceDemo 
{ 

    public  void show1 ();
     public  void Show2 (); 

} 

class DuoTaiFalse 
{ 

    void Method () 
    { 

        / ** 
        * place upward transition occurred. I.e. polymorphic 
        * / 
        InterfaceDemo inface = new new InterfaceDemo () { 

            public  void show1 () 
            { 

                System.out.println ( "Show1" ); 

            } 
            public  void Show2 () 
            { 

                System.out.println ( "Show2" );

            } 

            Public  void show3 () 
            { 

                System.out.println ( "Show3" ); 

            } 

        }; 

        inface.show1 (); 
        inface.show2 (); 
        / ** this place is wrong because InterfaceDemo inface = new InterfaceDemo () {} when carried to this place, it has already happened 
        * upward transition, that at this time, has not be able to call some other method other parent them. So call inface.show3 () will display an error 
        * / 
        // inface.show3 (); 

    } 


} 

class InnerClassDemo2 
{ 

    public  static  void main (String [] args) { 
        
        new new DuoTaiFalse () Method ();. 

    } 

}

 

Guess you like

Origin www.cnblogs.com/qq1312583369/p/10966530.html