当前位置:网站首页>Abstract classes and interfaces (study notes)
Abstract classes and interfaces (study notes)
2022-07-30 22:31:00 【bodybuilding class】
抽象类
当父类的一些方法不能确定时,可以用abstractThe keyword class decorates the method,这个方法就是抽象方法,用abstract来修饰该类就是抽象类
抽象类的介绍
1.用abstractThe keyword class when decorating a class,These classes are called abstract classes
访问修饰符 abstract 类名{}
2.用abstract关键字修饰一个方法时,这个方法就是抽象方法
访问修饰符 abstract 返回类型 方法名(参数列表);//没有方法体
3.The value of abstract classes is more of a design,After the designer has designed it,让子类继承并实现抽象类
Notes and details on the use of abstract classes
1.抽象类不能被实例化

2.抽象类不一定要包含abstract方法,也就是说,抽象类可以没有adstract方法
3.一旦类包含了abstract方法,则这个类必须声明为abstract

4.abstract只能修饰类和方法,不能修饰属性和其他的

5.抽象类可以有任意成员【Abstract classes are essentially classes】,比如:非抽象方法、构造器、静态属性
6.抽象方法不能有主体
7.如果一个类继承了抽象类,then it must implement all abstract methods,除非它自己也声明为abstract类
8.抽象方法不能使用private、final、static来修饰,因为这些关键字都是和重写相违背的
模板设计模式
最佳实践
public abstract class Template {
public abstract void job();
public void caleTimes(){
long start=System.currentTimeMillis();
job();
long end=System.currentTimeMillis();
System.out.println("执行时间:"+(end-start));
}
}
public class AA extends Template{
@Override
public void job() {
for (int i = 0; i < 1000000; i++) {
}
}
}
public class BB extends Template{
@Override
public void job() {
for (int i = 0; i < 2000000; i++) {
}
}
}
public class Demo {
public static void main(String[] args) {
AA aa = new AA();
aa.caleTimes();
BB bb = new BB();
bb.caleTimes();
}
}总结:same call,不同抽象
接口
接口就是给出一些没有实现的方法,封装到一起,when a class is used,在根据具体情况把这些方法写出来
interface 接口名{
//属性
//方法(抽象方法 默认实现方法 静态方法)
}
class 类名 implements 接口{
自己属性;
自己方法;
必须实现接口的抽象方法
}
在jdk7.0前 接口里的所有方法都没有方法体,即都是抽象方法
jdk8.0后可以有静态方法,默认方法,也就是说接口中可以有方法的具体实现
接口注意事项
1.接口不能被实例化
2.接口中所有的方法是 public方法,接口中抽象方法,可以不用adstract修饰
3.一个普通类实现接口,All methods of the open interface must be implemented
4.抽象类实现接口,可以不用实现接口的方法
5.一个类可以实现多个接口
6.接口的属性只能是final
7.接口中属性的访问形式:接口名.属性名
8.An interface cannot inherit a class that replaces it,But tourists must see more than one

9.接口的修饰符 只能是 public 和默认,这点和类的修饰符是一样的
接口实现和继承
接口和继承解决的问题不同
继承的价值主要在于:解决代码的复用性和可维护性
接口的价值主要在于:设计好各种规范,让其他类去实现这些方法
接口比继承更加灵活
Satisfy when inheritingis - a的关系,And the interface only satisfieslike - a的关系
接口在一定程度上实现代码解耦【接口规范性+动态绑定】
接口的多态特性
Interfaces have polymorphic transfer characteristics
小结
类的五大成员:1.属性 2.方法 3.构造器 4.代码块 5. 内部类
边栏推荐
猜你喜欢
随机推荐
navicat连接MySQL报错:1045 - Access denied for user ‘root‘@‘localhost‘ (using password YES)
MySQL压缩包方式安装,傻瓜式教学
IDEA 连接 数据库
MySQL compressed package installation, fool teaching
史上最全的Redis基础+进阶项目实战总结笔记
MySQL 5.7 detailed download, installation and configuration tutorial
科技的成就(三十一)
一文详解:SRv6 Policy模型、算路及引流
【MySQL】DQL相关操作
Regular expression syntax and usage
PhpMetrics usage
【高等数学】矩阵与向量组的秩和等价
TCP 连接 三次握手 四次挥手
Detailed explanation of the delete problem of ClickHouse delete data
MySQL 有这一篇就够(呕心狂敲37k字,只为博君一点赞!!!)
MySQL cursors
Go语学习笔记 - gorm使用 - gorm处理错误 Web框架Gin(十)
navicat无法连接mysql超详细处理方法
openim支持十万超级大群
ClickHouse删除数据之delete问题详解









