Notes to Java core technology Volume 1 Chapter 6 interface and internal classes anonymous internal classes and static internal classes

6.4.6 anonymous inner class

Suppose that when using variables, you can directly new a new object without naming it again. This local inner class that cannot afford the name of a class is called an anonymous inner class.

The writing method is: new interface name () {overwrite method () {method content}}

public class Main {

    public static void main(String[] args) throws CloneNotSupportedException, InterruptedException {
        Main solution = new Main();
        Date d = new Date();
       
        ActionListener tp = new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("the time is"+d);
                Toolkit.getDefaultToolkit().beep();
            }
        };
        Timer t = new Timer(10000,tp);
        t.start();
        JOptionPane.showMessageDialog(null,"Quit program?");
        System.exit(0);
    }
}

Objects created anonymously are usually put directly into the pass parameter, such as:

public class Main {
    public static void main(String[] args) throws CloneNotSupportedException, InterruptedException {
        Main solution = new Main();
        Date d = new Date();

        Timer t = new Timer(10000,new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent e) {
                System.out.println("the time is"+d);
                Toolkit.getDefaultToolkit().beep();
            }
        });
        t.start();
        JOptionPane.showMessageDialog(null,"Quit program?");
        System.exit(0);
    }
}

6.4.7 static internal class

Static inner class is to add a static to the inner class of the member. It means that the external class needs to reference the internal class variables or methods, and the internal class does not need to reference the external class variables and methods. Most of the internal classes used at ordinary times basically use static internal classes. For example, if there are multiple goods in an order, we will ask what goods are in the order, but if there is no order, do these goods still exist? It still exists. It can be found that whether the order exists or not, its associated internal class still exists. At this time, the method of static internal class can be selected.

import java.util.ArrayList;
import java.util.List;

public class Order {
    private List<Product> products;
    private int id;
    public Order(int id){
        this.id = id;
        products = new ArrayList<>();
    }
    public void addProduct(Product product){
        products.add(product);
    }

    public List<Product> getProductList(){
        return products;
    }

    public static class Product{
        String name;
        int cnt;
        public Product(String name, int cnt){
            this.name = name;
            this.cnt = cnt;
        }

        @Override
        public String toString() {
            return "Product{" +
                    "name='" + name + '\'' +
                    ", cnt=" + cnt +
                    '}';
        }
    }
}
public class Main {
    public static void main(String[] args) throws CloneNotSupportedException, InterruptedException {
        Main solution = new Main();
        Order.Product p = new Order.Product("Bean curd",1);
        Order.Product p2 = new Order.Product("Shallot",5);
        Order o = new Order(1);
        o.addProduct(p);
        o.addProduct(p2);
        System.out.println(o.getProductList());
    }
}

Series content:

Notes on Java core technology Volume 1: Chapter 1 overview of Java programming

Notes on Java core technology Volume 1: Chapter 2 Java programming environment

Notes on Java core technology Volume 1: Chapter III Basic programming structure of Java (1)

Notes on Java core technology Volume 1: Chapter III Basic programming structure of Java (2)

Notes on Java core technology Volume 1: Chapter III Basic programming structure of Java (3)

Notes on Java core technology Volume 1: Chapter III Basic programming structure of Java (4)

Notes on Java core technology Volume 1: Chapter III Basic programming structure of Java (5)

Notes on Java core technology Volume 1: Chapter III Basic programming structure of Java (6)

Notes on Java core technology Volume 1: Chapter 3 basic programming structure of Java (7) large number processing, array, multi-dimensional array and console parameter transmission

Notes on Java core technology Volume 1 Chapter 4: classes and objects

Chapter 4: classes and objects (2) Gregorian calendar and the basic composition of classes

Chapter 4: classes and objects (3) constructor global private methods

Chapter 4: classes and objects (4) static fields + static methods + factory methods

Chapter 4 of notes of Java core technology Volume 1: class and object (5) default value and default construction of formal parameter and argument constructor

Chapter 4: class and object (6) constructor call and initialization block

Chapter 4 notes of Java core technology Volume 1: class and object (7) annotation, JavaDoc and class design

Chapter 5 inheritance of notes of Java core technology Volume 1

Chapter 5 inheritance of notes of Java core technology Volume 1 (2)

Chapter 5 inheritance of notes of Java core technology Volume 1 (3)

Java core technology Volume 1 Notes Chapter 5 inheritance (4) equals method

Chapter 5 of notes of Java core technology Volume 1 inherits (5) hashCode and toString

Java core technology Volume 1 Notes Chapter 5 inheritance (6) generics

Java core technology Volume 1 Notes Chapter 5 inheritance (7) wrapper classes and variable arrays

Java core technology Volume 1 Notes Chapter 5 inheritance (8) enumerating classes and class reflection

Chapter 5 of notes of Java core technology Volume 1 inheritance (9) exception capture and reflection application

Chapter 5 inheritance (10) reflection in notes of Java core technology Volume 1

Java core technology Volume 1 Notes Chapter 5 inheritance (11) reflective generic array + method pointer + class design skills

Chapter 6 interfaces and internal classes in notes of Java core technology Volume 1

Notes to Java core technology Volume 1 Chapter 6 interface and internal classes (2)

Chapter 6 interface and internal class (3) interface callback and internal class

Notes to Java core technology Volume 1 Chapter 6 interfaces and internal classes (4) local internal classes and local internal classes reference method variable analysis

If you like, point a praise ~! Usually do the title, and notes will be updated to the official account.

Pay attention to official account and learn from each other: knowledge of Yu Niang Niang

 

 

Keywords: Java linq p2p

Added by hbalagh on Mon, 07 Feb 2022 00:00:25 +0200