java便利类,这个词偶然看到的,*找不到解释。就是抽象类继承接口。
比如Collection接口,里面有很多抽象方法,而AbstractCollection实现了Collection接口。jdk中如下描述这个便利类:
This class provides a skeletal implementation of the Collection interface, to minimize the effort required to implement this interface.
意思就是这个抽象类实现了接口中的大部分的方法,在需要实现Collection接口的时候继承这个类就可以专注实现自己需要的方法。
1:抽象类实现接口,可以实现接口中的所有方法,但这不是必须的。因为抽象类允许抽象方法存在,而接口中的方法都是抽象的。甚至,你可以写一个抽象类继续接口,但是什么都不做
public interface Interface {
public void method1();
public void method2();
public void method3();
}
public abstract class Abstract implements Interface{
}
很显然,这样做是没有什么意义的。
2:抽象类继承接口的最主要用处应该是“便利”了类实现接口必须实现所有的方法。比如说只想实现method1这个方法,如果直接实现接口的话,那么就需要实现所有的方法。但是有了抽象类之后,就可以在抽象类中实现所有方法,而继承这个抽象类的子类只需实现method1就可以了:
public interface Interface {
public void method1();
public void method2();
public void method3();
}
public abstract class Abstract implements Interface{
public void method1(){
}
public void method2(){
}
public void method3(){
}
}
public class Extend extends Abstract {
public void method1() {
}
}
3:抽象类实现接口主要用在事件监听方面
public interface MouseListener extends EventListener {
/**
* Invoked when the mouse button has been clicked (pressed
* and released) on a component.
*/
public void mouseClicked(MouseEvent e);
/**
* Invoked when a mouse button has been pressed on a component.
*/
public void mousePressed(MouseEvent e);
/**
* Invoked when a mouse button has been released on a component.
*/
public void mouseReleased(MouseEvent e);
/**
* Invoked when the mouse enters a component.
*/
public void mouseEntered(MouseEvent e);
/**
* Invoked when the mouse exits a component.
*/
public void mouseExited(MouseEvent e);
}
public abstract class MouseAdapter implements MouseListener {
/**
* Invoked when the mouse has been clicked on a component.
*/
public void mouseClicked(MouseEvent e) {}
/**
* Invoked when a mouse button has been pressed on a component.
*/
public void mousePressed(MouseEvent e) {}
/**
* Invoked when a mouse button has been released on a component.
*/
public void mouseReleased(MouseEvent e) {}
/**
* Invoked when the mouse enters a component.
*/
public void mouseEntered(MouseEvent e) {}
/**
* Invoked when the mouse exits a component.
*/
public void mouseExited(MouseEvent e) {}
}
protected class DoubleClickListener extends MouseAdapter {
// NOTE: This class exists only for backward compatability. All
// its functionality has been moved into Handler. If you need to add
// new functionality add it to the Handler, but make sure this
// class calls into the Handler.
Handler handler;
public DoubleClickListener(JList list) {
handler = new Handler(list);
}
/**
* The JList used for representing the files is created by subclasses, but the
* selection is monitored in this class. The TransferHandler installed in the
* JFileChooser is also installed in the file list as it is used as the actual
* transfer source. The list is updated on a mouse enter to reflect the current
* data transfer state of the file chooser.
*/
public void mouseEntered(MouseEvent e) {
handler.mouseEntered(e);
}
public void mouseClicked(MouseEvent e) {
handler.mouseClicked(e);
}
}
————————————————
版权声明:本文为CSDN博主「utopialxw」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/utopialxw/article/details/84271207