JAVA便利类
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
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!