mse

导航

抽象类继承接口的缘由

Posted on 2009-09-03 11:23  mse  阅读(662)  评论(0)    收藏  举报

/**
 *
 * 1) 在java中接口可以多继承,但类不可以多继承。
 * 2) 抽象类实现接口,抽象类可以全部实现接口的方法,但由于抽象类
  * 允许存在抽象方法,所以抽象类也可以不实现接口的方法。通常的的做法是抽象类实现接口的全部方法,然后客户只需继  承抽象类实现特定的方法即可。主要体现在awt的事件监听器方面。如 
  *public abstract class MouseAdapterextends
  *Objectimplements MouseListener, MouseWheelListener, MouseMotionListener
  *
  */
interface MathPlus {
 double getVol();
}

 

abstract class AllMath1 implements MathPlus {

 /*
  * public double getVol() { return 0; }
  */

}
附上jdk源代码:

public abstract class MouseAdapter implements MouseListener {
    /**
     * Invoked when the mouse has been clicked on a component.
     */
    public void mouseClicked(MouseEvent e) {}

    /**
     * Invoked when a mouse button has been pressed on a component.
     */
    public void mousePressed(MouseEvent e) {}

    /**
     * Invoked when a mouse button has been released on a component.
     */
    public void mouseReleased(MouseEvent e) {}

    /**
     * Invoked when the mouse enters a component.
     */
    public void mouseEntered(MouseEvent e) {}

    /**
     * Invoked when the mouse exits a component.
     */
    public void mouseExited(MouseEvent e) {}
}