Java syntax sugar -- anonymous inner class

1. Preface

Anonymous internal classes. Some friends think it is difficult to understand them. In fact, just as the name suggests.

Anonymous, indicating that this class has no name.

Internal class, which means that it is not an independent class, but a class within a class or method.

2. traditional methods

First of all, we need interface oriented programming, which is very natural. Interface oriented programming provides an abstraction of the real social model and can provide higher flexibility.

In the traditional way, we implement interfaces through classes, and then call them through class objects.

2.1 defining interfaces

For example, a war requires the use of weapons. Weapons need to be prepared and then launched. Therefore, the weapon interface is defined:

/**
 * Weapon interface
 */
public interface Weapon {
	/**
	 * prepare
	 */
	public void prepare();

	/**
	 * launch
	 */
	public void launch();
}

2.2 use interface

When the army launches a campaign, it needs to fight with weapons:

/**
 * Campaign
 */
public class Battle {
	/**
	 * Fight with weapons
	 */
	public void fight(Weapon w) {
		w.prepare();
		w.launch();
	}
}

2.3 using implementation classes

OK, at this time, if it is a modern army, there are many types of weapons, including tanks, artillery, aircraft carriers, aircraft, etc. we can realize various weapons and then fight.
The following example is the most common one. Define a class to implement an interface, and then call it through a class object.

/**
 * Tank weapon
 */
public class Tank implements Weapon {
	@Override
	public void prepare() {
		System.out.println("Tank loading shell");
	}

	@Override
	public void launch() {
		System.out.println("Tank fire");
	}
	
	public static void main(String[] args) {
		Battle battle=new Battle();
		Tank tank=new Tank();
		battle.fight(tank);
	}
}

3. use anonymous inner classes

However, if the implementation class of the interface is used only once, it is cumbersome for us to define classes and operate through class objects.

Specifically, in the above scene, there are no high-tech weapons at all, that is, a group of primitive people fight by throwing stones. At this point, it is not meaningful and troublesome to define a stone class alone, so you can directly use anonymous inner classes to solve the problem.

/**
 * Campaign
 */
public class Battle {
	/**
	 * Fight with weapons
	 */
	public void fight(Weapon w) {
		w.prepare();
		w.launch();
	}
	public static void main(String[] args) {
		Battle battle = new Battle();
		// Use anonymous inner classes
		battle.fight(new Weapon() {
			@Override
			public void prepare() {
				System.out.println("Pick up the stone");
			}
			@Override
			public void launch() {
				System.out.println("Stone throwing");
			}
		});
	}
}

Note that in the above example, we did not define the implementation class of the wepon interface, but directly created the object of the wepon type, and defined an anonymous inner class that implements the wepon interface.

Note that the anonymous inner class defines the method of the class at the same time, and then generates the object of this type, and then calls it. It is very concise and convenient.

If the above example is not easy to understand, you can refer to:

/**
 * Campaign
 */
public class Battle {
	/**
	 * Fight with weapons
	 */
	public void fight(Weapon w) {
		w.prepare();
		w.launch();
	}

	public static void main(String[] args) {
		Battle battle = new Battle();
		// Use anonymous inner classes
		Weapon weapon = new Weapon() {
			@Override
			public void prepare() {
				System.out.println("Pick up the stone");
			}

			@Override
			public void launch() {
				System.out.println("Stone throwing");
			}
		};
		battle.fight(weapon);
	}
}

4. usage scenarios

In most cases, you still use the structure of interface implementation class.

If it is only used temporarily during the call, it can be developed quickly with the help of anonymous internal classes without defining the implementation class.

Tags: Java

Posted by 5kyy8lu3 on Tue, 31 May 2022 00:45:56 +0530