黑马程序员技术交流社区
标题:
关于抽象的适配器类
[打印本页]
作者:
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:}
package java.awt.event;
/**
* An abstract adapter class for receiving window 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>WindowEvent</code> listener
* and override the methods for the events of interest. (If you implement the
* <code>WindowListener</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 Window using the window's <code>addWindowListener</code>
* method. When the window's status changes by virtue of being opened,
* closed, activated or deactivated, iconified or deiconified,
* the relevant method in the listener
* object is invoked, and the <code>WindowEvent</code> is passed to it.
*
* @see WindowEvent
* @see WindowListener
* @see <a >Tutorial: Writing a Window Listener</a>
*
* @author Carl Quinn
* @author Amy Fowler
* @author David Mendenhall
* @since 1.1
*/
public abstract class WindowAdapter
implements WindowListener, WindowStateListener, WindowFocusListener
{
/**
* Invoked when a window has been opened.
*/
public void windowOpened(WindowEvent e) {}
/**
* Invoked when a window is in the process of being closed.
* The close operation can be overridden at this point.
*/
public void windowClosing(WindowEvent e) {}
/**
* Invoked when a window has been closed.
*/
public void windowClosed(WindowEvent e) {}
/**
* Invoked when a window is iconified.
*/
public void windowIconified(WindowEvent e) {}
/**
* Invoked when a window is de-iconified.
*/
public void windowDeiconified(WindowEvent e) {}
/**
* Invoked when a window is activated.
*/
public void windowActivated(WindowEvent e) {}
/**
* Invoked when a window is de-activated.
*/
public void windowDeactivated(WindowEvent e) {}
/**
* Invoked when a window state is changed.
* @since 1.4
*/
public void windowStateChanged(WindowEvent e) {}
/**
* Invoked when the Window is set to be the focused Window, which means
* that the Window, or one of its subcomponents, will receive keyboard
* events.
*
* @since 1.4
*/
public void windowGainedFocus(WindowEvent e) {}
/**
* Invoked when the Window is no longer the focused Window, which means
* that keyboard events will no longer be delivered to the Window or any of
* its subcomponents.
*
* @since 1.4
*/
public void windowLostFocus(WindowEvent e) {}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2