Java 中接口和抽象类的区别
# Java 中接口和抽象类的区别
抽象类 | 接口 | |
---|---|---|
修饰符 | abstract | interface |
从设计层面来说 | 是一种模板设计,抽象类要被子类继承 | 是一种行为的规范,接口要被类实现 |
使用场景 | 在代码实现方面发挥作用,可以实现代码的重用 | 在系统架构设计方法发挥作用,主要用于定义模块之间的通信契约 |
抽象类-经典使用场景: StringBuilder 与 StringBuffer 都继承自 AbstractStringBuilder 类
abstract class AbstractStringBuilder implements Appendable, CharSequence {
/**
* The value is used for character storage.
*/
char[] value;
/**
* The count is the number of characters used.
*/
int count;
AbstractStringBuilder(int capacity) {
value = new char[capacity];
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
编辑 (opens new window)
上次更新: 2022/05/19, 21:26:01