本文介绍了什么是监听器和适配器之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想听众和适配器之间的区别。

I'm trying to differentiate between listeners and adapters.

他们是pretty大致相同,但听众你必须实现接口中的所有方法,但与适配器,你有一个选择只实现你需要这样code是清洁和更容易的方法阅读?

Are they pretty much the same but in listeners you have to implement all the methods in the interface, but with adapters you have an option to only implement the methods you need so the code is cleaners and easier to read?

我也得到了告诉适配器可以与实例只有一个实现,你不能实例听众,我不完全理解这一点。

I also got told that adapters enable instantiation with only one implementation and you can't instantiate listeners, I don't fully understand this.

能否有人请解释哪一个是更好地使用和事件,你可以用一个做,但你不能与其他?

Can someone please explain which one is better to use and things you can do with one but you can't with the other?

推荐答案

WindowListener为接口这迫使你覆盖所有方法,而WindowAdapter为实施的WindowListener ,而你只需要覆盖方法(S),你的兴趣来处理。

WindowListener is interface which force you to override all of the methods, while WindowAdapter is implementation of WindowListener and you only need to override the method(s) that you interest to deal with.

的WindowListener 是接口,它的意思是你不能实例化的WindowListener ,而 WindowAdapter的是您可以使用的具体类运算符来实例化。

WindowListener is interface which mean you cant instantiation the WindowListener, while WindowAdapter is concrete class that you can use new operator to instantiation.

当您使用 WindowAdapter的中,code更清洁您的类只覆盖需要的方法(S)。
例如:

When you use WindowAdapter, the code is more clean where your class only override the method(s) that you want.For example:

public class CloseListener implements WindowListener {
    // im not interest  on this event, but still need to override it
    @Override
    public void windowOpened(WindowEvent e) {

    }
    // im not interest  on this event, but still need to override it
    @Override
    public void windowClosing(WindowEvent e) {

    }

    @Override
    public void windowClosed(WindowEvent e) {
        System.exit(0);

    }
    // im not interest  on this event, but still need to override it
    @Override
    public void windowIconified(WindowEvent e) {

    }
    // im not interest  on this event, but still need to override it
    @Override
    public void windowDeiconified(WindowEvent e) {

    }

}

WindowAdapter的

在使用适配器code是清洁的:

WindowAdapter

While using adapter the code is cleaner:

// at JFrame class
addWindowListener(new CloseListener());

// reusable Close Listener
public class CloseListener extends WindowAdapter {
    @Override
    public void windowClosed(WindowEvent e) {
        System.exit(0);
    }
}

或者

addWindowListener(new WindowAdapter() {
    @Override
    public void windowClosed(WindowEvent e) {
         System.exit(0);
     }
});

因此​​,我建议使用 WindowAdapter的,而不是必须遵循。但是,有两个差不多只是 WindowAdapter的 API的存在是方便创建侦听器对象。

So I would recommend to use WindowAdapter, but not must follow. However, two of the API about the same just that WindowAdapter exists as convenience for creating listener objects.

编辑:

由于的WindowListener 接口,你可以在你的JFrame子类中实现它。

Since WindowListener is interface, you can implement it at your JFrame subclass.

public class MainWindow extends JFrame implements WindowListener {
    // this is ok
}
public class MainWindow extends JFrame, WindowAdapter {
    // this is not allow
}

但你不能用 WindowAdapter的做到这一点。

这篇关于什么是监听器和适配器之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 13:03