标签:interface abstract private 适配器 public
适配器模式:抽象类继承接口,重写其中不需要的方法为空方法,定义子类继承抽象类,重写没有实现的方法,这些方法就是我们需要的方法。
public interface ButtonListener { public void click(); public void dbclick(); public void keyUp(); public void keyDown(); } public abstract class ButtonAdapter implements ButtonListener { @Override public void keyUp() { } @Override public void keyDown() { } } public class Button { private ButtonListener bl; public void addListener(ButtonListener bl) { this.bl = bl; }; public void click() { bl.click(); } public void dbClick() { bl.dbclick(); } } class ButtonMove extends ButtonAdapter { @Override public void click() { System.out.println("click"); } @Override public void dbclick() { System.out.println("double click"); } } public static void main(String[] args) { Button b = new Button(); b.addListener(new ButtonMove()); b.click(); b.dbClick(); }
标签:interface abstract private 适配器 public
原文地址:http://11312010.blog.51cto.com/11302010/1771961