黑马程序员技术交流社区

标题: 关于抽象的适配器类 [打印本页]

作者: jing迪    时间: 2014-1-10 14:06
标题: 关于抽象的适配器类
适配器类是抽象类 抽象类不是不能建立对象吗??
但是这句话怎么理解啊
WindowAdapter wa = new WindowAdapter();
上面这句是错误的  提示抽象类不能创建对象这个倒是理解
但是下面这句
WindowAdapter wa = new WindowAdapter(){};
这句话居然没错 这是什么意思???创建WindowAdapter的对象??空参构造??? 对这句话不理解  谁能告诉我??

作者: 猎鹰tianya    时间: 2014-1-10 16:00
这种写是叫匿名内部类。格式:new <类或接口> <类的主体>
new WindowAdapter(){};
这句是的意思说:有一个类(匿名的),实现WindowAdapter这个抽象类,并返回一个这个类对象(不是WindowAdapter对象,抽象类不能new对象,只有实现它的类才能new对象)。

{}里面的内容是这个匿名类的主体,但看到这里空的,我也很纳闷!最起码的你应该把抽象类里的抽象方法都实现一下呀!
于是,把这句代码写到eclipse里面,Ctrl+单击==>打开源码,瞬间懂了,WindowAdapter下面有10个方法,但没有一个是抽象方法,所以这么写是可以的!
WindowAdapter wa=new WindowAdapter(){};这里还有传说中的多态 wa为父类引用

作者: 猎鹰tianya    时间: 2014-1-10 16:11

WindowAdapter源代码贴这里了,来,围观一下!
哎,,,,
说起源代码
心情很是复杂啊
想起了我的英语老师
为什么老师对我那么好
我却还是没把英语学好呢
{:soso_e118:}{:soso_e117:}{:soso_e131:}{:soso_e113:}{:soso_e132:}{:soso_e130:}{:soso_e122:}{:soso_e130:}

  1. package java.awt.event;

  2. /**
  3. * An abstract adapter class for receiving window events.
  4. * The methods in this class are empty. This class exists as
  5. * convenience for creating listener objects.
  6. * <P>
  7. * Extend this class to create a <code>WindowEvent</code> listener
  8. * and override the methods for the events of interest. (If you implement the
  9. * <code>WindowListener</code> interface, you have to define all of
  10. * the methods in it. This abstract class defines null methods for them
  11. * all, so you can only have to define methods for events you care about.)
  12. * <P>
  13. * Create a listener object using the extended class and then register it with
  14. * a Window using the window's <code>addWindowListener</code>
  15. * method. When the window's status changes by virtue of being opened,
  16. * closed, activated or deactivated, iconified or deiconified,
  17. * the relevant method in the listener
  18. * object is invoked, and the <code>WindowEvent</code> is passed to it.
  19. *
  20. * @see WindowEvent
  21. * @see WindowListener
  22. * @see <a >Tutorial: Writing a Window Listener</a>
  23. *
  24. * @author Carl Quinn
  25. * @author Amy Fowler
  26. * @author David Mendenhall
  27. * @since 1.1
  28. */
  29. public abstract class WindowAdapter
  30.     implements WindowListener, WindowStateListener, WindowFocusListener
  31. {
  32.     /**
  33.      * Invoked when a window has been opened.
  34.      */
  35.     public void windowOpened(WindowEvent e) {}

  36.     /**
  37.      * Invoked when a window is in the process of being closed.
  38.      * The close operation can be overridden at this point.
  39.      */
  40.     public void windowClosing(WindowEvent e) {}

  41.     /**
  42.      * Invoked when a window has been closed.
  43.      */
  44.     public void windowClosed(WindowEvent e) {}

  45.     /**
  46.      * Invoked when a window is iconified.
  47.      */
  48.     public void windowIconified(WindowEvent e) {}

  49.     /**
  50.      * Invoked when a window is de-iconified.
  51.      */
  52.     public void windowDeiconified(WindowEvent e) {}

  53.     /**
  54.      * Invoked when a window is activated.
  55.      */
  56.     public void windowActivated(WindowEvent e) {}

  57.     /**
  58.      * Invoked when a window is de-activated.
  59.      */
  60.     public void windowDeactivated(WindowEvent e) {}

  61.     /**
  62.      * Invoked when a window state is changed.
  63.      * @since 1.4
  64.      */
  65.     public void windowStateChanged(WindowEvent e) {}

  66.     /**
  67.      * Invoked when the Window is set to be the focused Window, which means
  68.      * that the Window, or one of its subcomponents, will receive keyboard
  69.      * events.
  70.      *
  71.      * @since 1.4
  72.      */
  73.     public void windowGainedFocus(WindowEvent e) {}

  74.     /**
  75.      * Invoked when the Window is no longer the focused Window, which means
  76.      * that keyboard events will no longer be delivered to the Window or any of
  77.      * its subcomponents.
  78.      *
  79.      * @since 1.4
  80.      */
  81.     public void windowLostFocus(WindowEvent e) {}
  82. }
复制代码








欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2