Java syntactic sugar-anonymous inner class

1 Introduction

Anonymous inner class, some friends find it difficult to understand, in fact, just as the name implies.

Anonymous means that this class has no name.

Inner class means that it is not an independent class, but a class inside a class or method.

2. Traditional way

First of all, we need interface-oriented programming. This is natural. Interface-oriented programming provides an abstraction of real social models and can provide higher flexibility.

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

2.1 Define the interface

For example, weapons need to be used in battle, and weapons need to be prepared and then launched, so define the weapon interface:

/**
 * 武器接口
 */
public interface Weapon {
    
    
	/**
	 * 准备
	 */
	public void prepare();

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

2.2 Use interface

When the army launches a battle, it needs to use weapons to fight:

/**
 * 战役类
 */
public class Battle {
    
    
	/**
	 * 使用武器进行战斗
	 */
	public void fight(Weapon w) {
    
    
		w.prepare();
		w.launch();
	}
}

2.3 Use implementation class

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

/**
 * 坦克武器
 */
public class Tank implements Weapon {
    
    
	@Override
	public void prepare() {
    
    
		System.out.println("坦克装填炮弹");
	}

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

3. Use anonymous inner classes

But if the implementation class of the interface is only used once, it is more cumbersome for us to define the class and operate through the class object.

Specific to the above scene, it means that there are no high-tech weapons at all, just a group of primitive men throwing stones to fight. At this point, if you define a stone class separately, it is of little significance and more troublesome, so you can directly use the anonymous inner class to solve it.

/**
 * 战役类
 */
public class Battle {
    
    
	/**
	 * 使用武器进行战斗
	 */
	public void fight(Weapon w) {
    
    
		w.prepare();
		w.launch();
	}
	public static void main(String[] args) {
    
    
		Battle battle = new Battle();
		// 使用匿名内部类
		battle.fight(new Weapon() {
    
    
			@Override
			public void prepare() {
    
    
				System.out.println("拿起石头");
			}
			@Override
			public void launch() {
    
    
				System.out.println("扔石头");
			}
		});
	}
}

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

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

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

/**
 * 战役类
 */
public class Battle {
    
    
	/**
	 * 使用武器进行战斗
	 */
	public void fight(Weapon w) {
    
    
		w.prepare();
		w.launch();
	}

	public static void main(String[] args) {
    
    
		Battle battle = new Battle();
		// 使用匿名内部类
		Weapon weapon = new Weapon() {
    
    
			@Override
			public void prepare() {
    
    
				System.out.println("拿起石头");
			}

			@Override
			public void launch() {
    
    
				System.out.println("扔石头");
			}
		};
		battle.fight(weapon);
	}
}

4. Usage scenarios

In most cases, the interface-implementation class structure is still used.

If it is only used temporarily when invoking, it is not necessary to define the implementation class and use anonymous inner classes to develop quickly.

Guess you like

Origin blog.csdn.net/woshisangsang/article/details/107159686