Anonymous inner class detail in java

  • 2020-07-21 07:41:56
  • OfStack

java anonymous inner class:

1: Anonymous inner class. Anonymous inner class is an inner class without a name.

2: The role of anonymous inner classes

Because there is no name, anonymous inner classes can only be used once and are often used to simplify coding.

3: Implementation of anonymous inner classes

There are two ways to implement anonymous inner classes: first, inherit a class and override its methods; Second, implement an interface (which can be multiple) and implement its methods.

4: Creation of anonymous inner classes

Anonymous classes are classes that cannot have names, so there is no way to refer to them. They must be declared as part 1 of the new statement when created.


package com.mianshi.test;
/**
 *  The class name: AnonymousInnerClassTest 
 *  Description:   Anonymous inner class tests 
 *  Founder: Wang Qiulin  
 *  Creation time: 2017-2-12
 */
public class AnonymousInnerClassTest {
  public static void main(String args[]){   
    AnonymousInnerClassTest test = new AnonymousInnerClassTest();   
    test.show();   
  }   
  // Constructed in this method 1 Anonymous inner classes    
  private void show(){   
    Out anonyInter = new Out(){// Gets an anonymous inner class instance    
      void show(){// Override the methods of the parent class    
        System.out.println("this is Anonymous InnerClass showing.");
      }   
    };   
    anonyInter.show();// Call its method    
  } 
}
// This is a 1 An anonymous inner class will get an additional implementation by overriding its methods    
class Out{   
  void show(){   
    System.out.println("this is Out showing.");   
  }   
}

5: Basic implementation of anonymous inner classes

(1) Abstract method implementation


abstract class Person {
  public abstract void eat();
}
public class Demo {
  public static void main(String[] args) {
    Person p = new Person() {
      public void eat() {
        System.out.println("eat something");
      }
    };
    p.eat();
  }
}

Operation results: eat something

(2) Interface implementation


interface Person {
  public void eat();
}
public class Demo {
  public static void main(String[] args) {
    Person p = new Person() {
      public void eat() {
        System.out.println("eat something");
      }
    };
    p.eat();
  }
}

Operation results: eat something

As you can see from the above example, methods in subclasses can be implemented using anonymous inner classes as long as a class is abstract or has an interface. The most common case is on multithreaded implementations, because to implement multithreading you must inherit from the Thread class or the Runnable interface.

(3) The anonymous inner class implementation of Thread class


public class Demo {
  public static void main(String[] args) {
    Thread t = new Thread() {
      public void run() {
        for (int i = 1; i <= 5; i++) {
          System.out.print(i + " ");
        }
      }
    };
    t.start();
  }
}

Operation results: 1 2 3 4 5

(4) Anonymous inner class implementation of Runnable interface


public class Demo {
  public static void main(String[] args) {
    Runnable r = new Runnable() {
      public void run() {
        for (int i = 1; i <= 5; i++) {
          System.out.print(i + " ");
        }
      }
    };
    Thread t = new Thread(r);
    t.start();
  }
}

Operation results: 1 2 3 4 5

2. Considerations for anonymous inner classes

(1) Anonymous inner classes cannot have constructors.

(2) Anonymous inner classes cannot define any static members, methods, or classes.

(3) is an anonymous inner class can't public, protected, private, static.

(4) Only one instance of the anonymous inner class can be created.

(5) An anonymous inner class 1 must be at the end of new, with its implicit implementation of an interface or implementation of a class.

(6) Because the anonymous inner class is a local inner class, all restrictions of the local inner class apply to it.

(7) this in anonymous and inner classes: Sometimes we use inner classes and anonymous classes. When used in anonymous classes, this refers to the anonymous class or inner class itself. If we want to use the methods and variables of the external class, we should add the class name of the external class.

Above is the content of this article, the need of friends can refer to


Related articles: