当前位置:网站首页>抽象类和接口的区别
抽象类和接口的区别
2022-07-19 05:15:00 【露棉】
抽象类:(Abstract)
1,概念:如果一个类定义了方法,但没有具体执行代码,那么这个方法叫抽象方法,抽象方法用abstract来修饰,由于无法执行抽象方法,则这个类也被叫做抽象类。用abstract修饰的类就是抽象类。
eg1:
// 抽象类
abstract class Person {
// 抽象方法
public abstract void say();
}
特点:1.抽象类不会被实例化
eg2:Person p = new Person(); // 编译错误
2.抽象方法无方法体
eg3:public abstract void run();
接口:
1,概念:接口是对行为的抽象,它是抽象方法的集合,利用接口可以达到行为定义和业务实现分离的架构设计目的,并且接口的多重实现以及多重继承等特性,在架构设计上更灵活,易扩展。接口是比抽象类更加抽象的一种纯抽象的代码结构,不能包含成员变量,只包含常量,抽象方法,默认方法等。
区别:
1.在接口中定义抽象方法可以不加abstract关键字,而抽象方法必须加。
2.在接口中不存在普通方法,不允许构造方法,声明的常量不允许被修改,而抽象类可以存在普通方法,允许构造方法,可以定义成员变量。
3.在接口中可以多重继承,使用implements多个接口,而抽象类只能继承一个类。
4.在接口中只能使用public访问修饰符,而抽象类可以使用任意访问修饰符。
边栏推荐
猜你喜欢
Chrome developer tool shortcut key reference to improve development efficiency
Solution of STM32 cubeide breakpoint failure
Detailed explanation of multiresolution decomposition and reconstruction of wavelet analysis
After ise14.7 was installed in win10 system, the software flashed back and the driver could not be installed normally
Infrared decoding program based on timer capture function (NEC protocol)
Reasons and solutions for the whitening of computer shortcuts -- the lesson of blood woo woo
UltraEdit line break / tab settings
FPGA skimming p1:1-out-of-4 multiplexer, asynchronous reset Series T trigger, parity check, shift splicing multiplication
多基线干涉仪系统测向误差分析
Openstack uses dashboard to perform image operations, manage instances, and other operations
随机推荐
Leetcode:14. 最长公共前缀【思维+排序】
2022-7-11 第八小组 顾宇佳 学习笔记(Js)
Verilog HDL language summary (full)
vulnhub 靶機 Jangow: 1.0.1
Solution of STM32 cubeide breakpoint failure
vulnhub 靶机 GOLDENEYE: 1
雷达基础知识
Vulnhub target jangow: 1.0.1
Thinkphp5 verification code
FPGA decoder + decoder (including code)
详细讲解JS中的加法(+)运算,基本数据类型相加,引用数据类型相加底层的运算规则,[]+{},{}+[]
Unity实用框架(三)事件系统
Leetcode: Game 302 (summary)
計算機408+數據庫【適合考研複試或期末複習】
FPGA skimming P4: use 8-3 priority encoder to realize 16-4 priority encoder, use 3-8 decoder to realize full subtracter, realize 3-8 decoder, use 3-8 decoder to realize logic function, and use data se
Qt字符串操作
Connaissance de la technologie des tunnels d'infiltration Intranet
2022-7-12 第八小组 顾宇佳 (Js)
乐山师范程序设计大赛2020-B: 设计网页【判素数】
乐山师范程序设计大赛2020-E: 分石头【01背包】