Article From:https://www.cnblogs.com/yanquebbuyan/p/9971468.html

1.Membership inner class (declared inside class & amp; & amp; outside method)

class Person{
    String name = "Han Meimei;
    int age;
    class Bird{
        String name = "Oriole ";
        int id;
        void setName(String name){
            System.out.println(name);//Method Introduced Name-Rhododendron
            System.out.println(this.name);//BirdObject name-oriole
            System.out.println(Person.this.name);//PersonObject name-Han Meimei

        }
        void info(){
            show();
        }

    }
    static class Dog{

    }
    void show(){
        System.out.println("I'm the show () method.);
    }

    public static void main(String[] args) {
        Person.Dog dog = new Person.Dog();//Object Generation Method for Static Internal Classes
        Person.Bird bird = new Person().new Bird();//Object Construction Method of Non-static Internal Classes
        bird.info();
        bird.setName("Rhododendron ");
    }

}

2.Local internal classes (declared within methods)

class OuterClass{
    void method(){

        class InnerClass{

        }
    }
    Comparable getComparable(){//Inner class
        class MyComparable implements Comparable{
            @Override
            public int compareTo(Object o) {
                return 0;
            }
        }return new MyComparable();
    }
    Comparable getComparable1(){//Anonymous Inner Class
        return new Comparable() {
            @Override
            public int compareTo(Object o) {
                return 0;
            }
        };
    }
}

3.Application examples of anonymous inner classes

public class Main3 {
    public static void main(String[] args) {
        //The relationship between the method class and the interface is clear and easy to read.
       Main3 main3 = new Main3();
       NoteBook noteBook = new NoteBook();
       main3.show(noteBook);
       
       //This approach hides the relationship between classes and interfaces.
       main3.show(new Product() {
           @Override
           public void getName() {
               System.out.println("Iphone5s");
           }

           @Override
           public void getPrice() {
               System.out.println("5288");
           }
       });
       //This way hides the class, but the interface operates clearly.
       Product product = new Product() {
           @Override
           public void getName() {
               System.out.println("Galaxy Note3");
           }

           @Override
           public void getPrice() {
               System.out.println("5288");
           }
       };
        System.out.println();
        Product product1 =main3.getProduct();
        product1.getName();
        product1.getPrice();

        Product product2 =main3.getProduct();
        product2.getName();
        product2.getPrice();

    }
    void show(Product product){
        product.getName();
        product.getPrice();
    }
    Product getProduct(){
      class Camera implements Product{

          @Override
          public void getName() {
              System.out.println("Digital Camera);
          }

          @Override
          public void getPrice() {
              System.out.println("400");

          }
 }return new Camera();
    }
      Product getProduct1(){
          return new Product(){

              @Override
              public void getName() {
                  System.out.println("E-book ");
              }

              @Override
              public void getPrice() {
                  System.out.println("1000");
              }
          };
}
}
interface Product{
    void getName();
    void getPrice();
}
class NoteBook implements Product{

    @Override
    public void getName() {
        System.out.println("HPNotebook ");
    }

    @Override
    public void getPrice() {
        System.out.println("¥5000");
    }
}

The above content comes from the 135th volume of the java course of the Cereal Academy. Website http://www.gulixueyuan.com/course/39/task/443/show

Leave a Reply

Your email address will not be published. Required fields are marked *