A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© jing迪 高级黑马   /  2014-1-10 14:06  /  1308 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

适配器类是抽象类 抽象类不是不能建立对象吗??
但是这句话怎么理解啊
WindowAdapter wa = new WindowAdapter();
上面这句是错误的  提示抽象类不能创建对象这个倒是理解
但是下面这句
WindowAdapter wa = new WindowAdapter(){};
这句话居然没错 这是什么意思???创建WindowAdapter的对象??空参构造??? 对这句话不理解  谁能告诉我??

评分

参与人数 1技术分 +1 收起 理由
FFF + 1 赞一个!

查看全部评分

2 个回复

倒序浏览
这种写是叫匿名内部类。格式:new <类或接口> <类的主体>
new WindowAdapter(){};
这句是的意思说:有一个类(匿名的),实现WindowAdapter这个抽象类,并返回一个这个类对象(不是WindowAdapter对象,抽象类不能new对象,只有实现它的类才能new对象)。

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

评分

参与人数 1技术分 +1 收起 理由
FFF + 1 神马都是浮云

查看全部评分

回复 使用道具 举报

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. }
复制代码



回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马