java中Adapter是什么概念

    Adapter乃适配器,适配不协调之两者,看维基一解释:

 

A device or application used to achieve operative compatibility between devices that otherwise are incompatible.

He had an adapter that let him plug his phone into the car's cigarette lighter for power.

说的是手机和车载电源插座之间的适配器连接了本身不协调的两者,这就是适配器adapter的功能。

 

JDK DOC中对KeyAdapter的解释:

public abstract class KeyAdapter

extends Object
implements KeyListener
 

An abstract adapter class for receiving keyboard events. The methods in this class are empty. This class exists as convenience for creating listener objects.

Extend this class to create a KeyEvent listener and override the methods for the events of interest. (If you implement the KeyListener interface, you have to define all of the methods in it. This abstract class defines null methods for them all, so you can only have to define methods for events you care about.)

Create a listener object using the extended class and then register it with a component using the component's addKeyListener method. When a key is pressed, released, or typed, the relevant method in the listener object is invoked, and the KeyEvent is passed to it.

 

这里它适配了什么呢?原本如果没有KeyAdapter,那就用实现了KeyListener接口的类来作listener,这样就不得不实现许多不需要实现的方法,逻辑上是不适配的,所以KeyAdapter应运而生,适配!

 

 

你可能感兴趣的:(java,jdk)