错了
适配器不是抽象类
适配器是一个实现了借口的具体的类
他已经帮你写好了所有需要实现的方法
但是他的实现事实上是什么事情也不做比如说
在接口 WindowListener 是这样定义的:
    public void windowOpened(WindowEvent e);
    public void windowClosing(WindowEvent e);
    public void windowClosed(WindowEvent e);
    public void windowIconified(WindowEvent e);
    public void windowDeiconified(WindowEvent e);
    public void windowActivated(WindowEvent e);而在适配器 WindowAdapter 重视这样实现的
    public void windowOpened(WindowEvent e) {}
    public void windowClosing(WindowEvent e) {}
    public void windowClosed(WindowEvent e) {}
    public void windowIconified(WindowEvent e) {}
    public void windowDeiconified(WindowEvent e) {}
    public void windowActivated(WindowEvent e) {}看 WindowAdapter 适配器实现了Windowlistener接口中的所有方法,WindowAdapter 适配器是一个具体的类,但是注意它是怎样实现的呢?仅仅是一个"{}"而已,也就是说它虽然实现了那些方法,但事实上是什么也不做那怎么用呢?就是你自己定义一个类继承WindowAdapter这个类,并且重写其中某个你需要用到的方法所谓不用实现所有方法这种说法是概念错误的实际上是你不需要重写所有方法,而只需要重写你要用到的方法就可以了

解决方案 »

  1.   

    下面是一个接口类的代码。你可以清楚的看到,实际上FocusAdapter已经给出了FocusListener的所有的方法一个缺省的实现(也就是什么也不做),你只要继承接口类,并且用自己具体的方法重载接口类里面你需要的方法。可以帮你节省时间(也就是偷懒)。
    为什么要把接口类声明成abstract呢?其实,接口类里面没有一个abstract的方法(也就是不强制用户类必须实现某些特定的方法)。把它声明成abstract其实是为了阻止用户直接实例化接口类,因为接口类是空实现。实例化了没有任何意义。/*
     * @(#)FocusAdapter.java 1.14 01/12/03
     *
     * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
     * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
     */package java.awt.event;/**
     * An abstract adapter class for receiving keyboard focus events.
     * The methods in this class are empty. This class exists as
     * convenience for creating listener objects.
     * <P>
     * Extend this class to create a <code>FocusEvent</code> listener 
     * and override the methods for the events of interest. (If you implement the 
     * <code>FocusListener</code> 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.)
     * <P>
     * Create a listener object using the extended class and then register it with 
     * a component using the component's <code>addFocusListener</code> 
     * method. When the component gains or loses the keyboard focus,
     * the relevant method in the listener object is invoked,
     * and the <code>FocusEvent</code> is passed to it.
     *
     * @see FocusEvent
     * @see FocusListener
     * @see <a href="http://java.sun.com/docs/books/tutorial/post1.0/ui/focuslistener.html">Tutorial: Writing a Focus Listener</a>
     * @see <a href="http://www.awl.com/cp/javaseries/jcl1_2.html">Reference: The Java Class Libraries (update file)</a>
     *
     * @author Carl Quinn
     * @version 1.14 12/03/01
     * @since 1.1
     */
    public abstract class FocusAdapter implements FocusListener {
        /**
         * Invoked when a component gains the keyboard focus.
         */
        public void focusGained(FocusEvent e) {}    /**
         * Invoked when a component loses the keyboard focus.
         */
        public void focusLost(FocusEvent e) {}
    }
      

  2.   

    danceflash(Wine) ,很对不起,你也错了!
      

  3.   

    接口类(也就是你说的适配器)全部是abstract。你没有看我的说明吗?呵呵
      

  4.   

    虽然声明为abstract的,但只是为了不让他有具体的实例,因为他根本就没有必要有具体的实例。
      

  5.   

    同意bluesmile979(笑着),这是一个不错的Design
      

  6.   

    bluesmile979(笑着),你的意思是不是说虽然适配器里的方法是abstract,但我继承时不用全部实现,是么?
      

  7.   

    shotgun79(炮炮龙) :是的,你只需要实现你感兴趣的方法。就算你一个方法也不实现也是可以的,只是空继承。但是这样没有意义!
      

  8.   

    恩?
    错了啊
    虽然适配器被声明为abstract的
    但是适配器类中所有的方法都是非abstract的方法
    之所以适配器类被声明为abstract的,是要强制你去重写其中的方法而已