Section 4 Anonymous Internal Classes

Section 4 Anonymous Internal Classes

Anonymous internal class, that is, internal class without class name

1. Inheritance-based anonymous internal classes

grammar

New <corresponding constructor of parent class to inherit from anonymous inner class> {
    //Fields and methods of anonymous internal classes
};
  1. The above syntax declares both an internal class and creates an anonymous internal class object (objects cannot be created later because they have no class name)
  2. You can override the methods of the parent class and define your own methods and fields in anonymous internal classes, but these methods and fields can only be used in anonymous internal classes because anonymous internal class object references cannot be created

Objects of anonymous internal classes are used through polymorphism, pointing to their objects with references to their directly or indirectly inherited parent classes

So by reference only the parent method whose override can be invoked, its own methods and fields cannot be used and can only be used internally

Define Anonymous Internal Class

public class CreateNotNameInnerClass {
	public static void main(String[] args) {
        // Define inherited Student anonymous inner class
		Student s = new Student() {
			@Override
			public void sayHello() {
				System.out.println("Invoke anonymous inner class!");
			}
		};
		
        // Call override method through polymorphism
		s.sayHello();
	}
}

class Student{
	public void sayHello(){
		System.out.println("Hello!");
	}
}

/*
 * Run Results
 * Invoke anonymous inner class!
 */

2. Anonymous internal class based on interface implementation

grammar

New <interface name> (){
	//Anonymous internal class fields and methods
}
  1. Similar to an anonymous internal class based on inheritance, but not a constructor after new, but an anonymous internal class that implements an interface
  2. Since anonymous internal classes have no name and all anonymous internal classes cannot be abstract, all non-default methods in the interface must be implemented

Define Anonymous Internal Class

public class CreateNotNameInnerClass {
	public static void main(String[] args) {
        // Define an anonymous internal class that implements the Person interface
		Person p = new Person() {
			@Override
			public void sayHello() {
				System.out.println("Calling sayHello() successfully!");
			}

			@Override
			public void run() {
				System.out.println("Calling run() successfully!");
			}
		};
		
		p.sayHello();
		p.run();
	}
}

interface Person{
	void run();
	void sayHello();
}

/*
 * Run Results
 * Calling sayHello() successfully!
 * Calling run() successfully!
 */

Initialization of anonymous internal classes

The constructor cannot be written because the anonymous inner class has no class name, but the anonymous inner class can be initialized using a nonstatic statement block

public class CreateNotNameInnerClass {
	public static void main(String[] args) {
		A a = new A() {
            // Define non-static statement initialization block
			{
				System.out.println("Execute a non-static statement block");
				var = 520;
			}
		};
		
		System.out.println(a.var);
	}
}

abstract class A{
	int var;
}

/*
 * Run Results
 * Execute a non-static statement block
 * 520
 */

Use of anonymous internal classes

In actual development, many classes are used only once, and anonymous internal classes can be used without creating multiple objects

The following example uses an anonymous internal class to write a comparator

import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;

public class TestNotNameInnerClass {
	public static void main(String[] args) {
		// Create an age comparator
        // Instances of this class are used only once
		Comparator<Student> cmp = new Comparator<Student>() {
			@Override
			public int compare(Student o1, Student o2) {
				return o1.getAge() - o2.getAge();
			}
		};
		
		Set<Student> set = new TreeSet<>(cmp);
		set.add(new Student(1001, 18));
		set.add(new Student(1000, 15));
		set.add(new Student(1048, 23));
		set.add(new Student(1451, 12));
		
		System.out.println(set);
	}
}

class Student implements Comparable<Student>{
	private int id;
	private int age;
	
	public Student(int id, int age) {
		this.id = id;
		this.age = age;
	}
	
	public int getAge() {
		return this.age;
	}
    
    public int getId(){
		return this.id;
    }
	
	@Override
	public int compareTo(Student o) {
		// id comparator
		return this.id - o.getId();
	}
	
	@Override
	public String toString() {
		return "[ ID: " + this.id + ", Age: " + this.age + " ]";
	}
}

/*
 * Running results (sorted by age)
 * [[ ID: 1451, Age: 12 ], [ ID: 1000, Age: 15 ], [ ID: 1001, Age: 18 ], [ ID: 1048, Age: 23 ]]
 */

Tags: Java

Posted on Sun, 07 Jun 2020 21:28:57 -0400 by aravona