Topic: [求助]关于这个程序有点想不通?

  Print this page

1.[求助]关于这个程序有点想不通? Copy to clipboard
Posted by: guliusuo
Posted on: 2006-09-21 16:48

import java.awt.*;
import java.awt.event.*;
public class W2 extends Frame{
W2(){
super("管理系统");
setSize(750,550);
setVisible(true);
addWindowListener(new Win());
}
class Win extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(0);
}
}
public static void main(String args[]){
new W2();
}
}
疑问:
WindowAdapter的定义:
public abstract class WindowAdapter extends Object implements WindowListener{
public 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);
public void windowDeactivated(WindowEvent e);
}
WindowAdapter是个抽象类,程序中的子类Win只覆盖了父类(也就是这里的抽象类)中的一个方法 public void windowClosing(WindowEvent e),这样的Win类按道理应该还是抽象类,但是在这个程序里却可以将其实例化。而且很奇怪,它具然可以通过编译,并运行!
有哪位高手知道这是怎么回事!!!
请求帮助!

2.Re:[求助]关于这个程序有点想不通? [Re: guliusuo] Copy to clipboard
Posted by: zcjl
Posted on: 2006-09-21 17:41

也许是版本不一致吧,我用jdk1.5,WindowAdapter的定义是,其中没有一个抽象方法:
public abstract class WindowAdapter
implements WindowListener, WindowStateListener, WindowFocusListener
{
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) {}
public void windowDeactivated(WindowEvent e) {}
public void windowStateChanged(WindowEvent e) {}
public void windowGainedFocus(WindowEvent e) {}
public void windowLostFocus(WindowEvent e) {}
}

3.Re:[求助]关于这个程序有点想不通? [Re: guliusuo] Copy to clipboard
Posted by: gotoo
Posted on: 2006-09-21 17:50

windowlistener是接口类,windowAdapter类是实现其中方法的子类,用的时候覆盖要用的方法就行了

4.Re:[求助]关于这个程序有点想不通? [Re: guliusuo] Copy to clipboard
Posted by: JiafanZhou
Posted on: 2006-09-21 23:34

It means you are not familiar with the polymorphism.

The reason why WindowAdapter is an abstract class is that this class has no real function but an adaptor ( an implementation of WindowListener ).

The subclass who inherits this abstract class can be either an abstract or concrete class...in your case..it is a concrete inner class, not an abstract class.

Regards,
Jiafan

5.Re:[求助]关于这个程序有点想不通? [Re: guliusuo] Copy to clipboard
Posted by: guliusuo
Posted on: 2006-09-22 07:31

谢谢各位了,可能是版本上的问题!

6.Re:[求助]关于这个程序有点想不通? [Re: guliusuo] Copy to clipboard
Posted by: cxp108
Posted on: 2006-09-22 09:02

Adapter类本来就是没有任何抽象方法的。

有的时候你仅需要Listener中的某个函数,但是编译器强制要求实现Listener的所有函数,否则代码无法通过编译。一般情况下你只能通过实现空的(一行代码都没有)Listener中要求的函数来保证代码通过编译。

这样的代码很难看,而且也很麻烦。

Adapter就是一个默认实现了Listener接口的类,它实现的所有函数都是空的(一行代码都没有),其实Adapter只是代你写了一些空的函数而已,当你继承Adapter的时候,你就拥有了一个完整实现Listener的类,不需要你再一个一个的声明Listener中的函数,这样代码立刻就可以通过编译了。
当继承Adapter的时候,你要做的仅仅是将你需要的函数覆写就可以了。

7.Re:[求助]关于这个程序有点想不通? [Re: guliusuo] Copy to clipboard
Posted by: xudeliang
Posted on: 2006-09-27 09:51

WindowAdapter没有抽象方法。所以可以实例化

8.Re:[求助]关于这个程序有点想不通? [Re: guliusuo] Copy to clipboard
Posted by: liangx
Posted on: 2006-09-27 11:33

这里有个例子

abstract class Base{}
class Derive extends Base{}

public class Test{
public static void main(String []s){
Base b=new Base(); //会出错
Derive d=new Derive() //正确通过编译
}
}


重以上的例子可以看出。对于用abstract修饰的类,无论有没有抽象方法
都无法实例化。阁下估计没有仔细去看JAVA的语法规范才会问这个问题。

9.Re:[求助]关于这个程序有点想不通? [Re: guliusuo] Copy to clipboard
Posted by: guliusuo
Posted on: 2006-09-30 13:08

学习了一下,懂了!谢谢各位的帮忙了!


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923