当前位置:网站首页>抽象类和接口
抽象类和接口
2022-08-11 07:35:00 【crazy__xieyi】
目录
一、抽象类
- 1.语法规则
- 2.抽象类的特性
- 3.作用
二、接口
- 1.语法规则
- 2.接口的使用
- 3.接口特性
- 4.实现多个接口
- 5.接口间的继承
三、抽象类和接口的区别
总结
一、抽象类
1.语法规则
// 抽象类:被abstract修饰的类
public abstract class Shape {
// 抽象方法:被abstract修饰的方法,没有方法体
abstract public void draw();
abstract void calcArea();
// 抽象类也是类,也可以增加普通方法和属性
public double getArea(){
return area;
}
protected double area; // 面积
}
2.抽象类的特性
3.作用
二、接口
1.语法规则
接口的定义格式与定义类的格式基本相同,将class关键字换成 interface 关键字,就定义了一个接口。
public interface 接口名称{
// 抽象方法
public abstract void A(); // public abstract 是固定搭配,可以不写
public void B();
abstract void C();
void D();
// 注意:在接口中上述写法都是抽象方法,推荐方式4( void D();),代码更简洁
}
2.接口的使用
接口不能直接使用,必须要有一个"实现类"来"实现"该接口,实现接口中的所有抽象方法。子类和父类之间是extends 继承关系,类与接口之间是implements 实现关系。
3.接口特性
1. 接口不能实例化。
4.实现多个接口
5.接口间的继承
interface A extends A1, A2, A3 {
}
三、抽象类和接口的区别
抽象类和接口都是 Java 中多态的常见使用方式。
总结
抽象类:
1.抽象类使用abstract修饰。
2.抽象类中可以包含普通类中所包含的成员。
3.抽象类与普通类不一样的是,抽象类可以包含抽象方法。
4.抽象方法使用abstract修饰,抽象方法可以没有具体的实现。
5.不能实例化抽象类。
6.抽象类存在的意义就是为了被继承。
7.如果一个普通类继承了一个抽象类,那么普通类就必须重写抽象类当中的方法。
8.如果一个抽象类A继承了一个抽象类B,此时A当中不需要重写B当中的抽象方法,但是如果A再被普通类继承,就需要重写。
9.抽象方法不能是私有的,要满足重写的规则。
10.抽象类当中可以有构造方法,是为了方便子类能够调用,来初始化抽象类中的成员。
接口:
1.使用interface来修饰接口。
2.接口当中的成员方法不能有具体的实现:默认是public abstract 的方法;JDK8开始,允许有可以实现的方法,但这个方法只能是由default修饰。可以实现有一个静态方法。
3.接口中的成员变量默认是 public static final 修饰的。
4.接口不能被实例化。
5.类与接口之间采用implements来实现多个接口。
6.子类重写抽象方法,必须加上public。
7.接口中不能有静态代码块和构造方法。
8.如果不想实现接口的方法,可以把这个类定义为抽象类,但是这个类如果被其它类继承,就需要重写。
9.一个类可以实现多个接口,使用implements,用逗号隔开,这样可以解决多继承的问题。
边栏推荐
- 9、Neural Sparse Voxel Fields
- Activity的四种状态
- Redis source code-String: Redis String command, Redis String storage principle, three encoding types of Redis string, Redis String SDS source code analysis, Redis String application scenarios
- go-grpc TSL认证 解决 transport: authentication handshake failed: x509 certificate relies on ... ...
- Redis source code: how to view the Redis source code, the order of viewing the Redis source code, the sequence of the source code from the external data structure of Redis to the internal data structu
- 接口测试的基础流程和用例设计方法你知道吗?
- Item 2 - Annual Income Judgment
- 1036 Programming with Obama (15 points)
- 1036 跟奥巴马一起编程 (15 分)
- 1101 B是A的多少倍 (15 分)
猜你喜欢
少年成就黑客,需要这些技能
leetcode: 69. Square root of x
tf.reduce_mean()与tf.reduce_sum()
Tf中的平方,多次方,开方计算
oracle数据库中列转行,列会有变化
1002 Write the number (20 points)
1081 检查密码 (15 分)
Square, multi-power, square root calculation in Tf
The most complete documentation on Excel's implementation of grouped summation
tf.cast(), reduce_min(), reduce_max()
随机推荐
Redis source code: how to view the Redis source code, the order of viewing the Redis source code, the sequence of the source code from the external data structure of Redis to the internal data structu
1.1-Regression
【43. 字符串相乘】
Pico neo3在Unity中的交互操作
关于#sql#的问题:怎么将下面的数据按逗号分隔成多行,以列的形式展示出来
Keep track of your monthly income and expenses through bookkeeping
通过记账,了解当月收支情况
1051 Multiplication of Complex Numbers (15 points)
接口测试的基础流程和用例设计方法你知道吗?
4.1-支持向量机
国密规范 SM2 SM3 SM4
cdc连sqlserver异常对象可能有无法序列化的字段 有没有大佬看得懂的 帮忙解答一下
1101 B是A的多少倍 (15 分)
easyrecovery15数据恢复软件收费吗?功能强大吗?
3.2 - classification - Logistic regression
JUC并发编程
支持各种文件快速重命名最简单的小技巧
Distributed Lock-Redission - Cache Consistency Solution
9、Neural Sparse Voxel Fields
【LeetCode】Summary of linked list problems