当前位置:网站首页>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. 内部类
边栏推荐
- matlab标量场作图
- MySQL compressed package installation, fool teaching
- TransGAN code reproduction - Jiutian Bisheng Platform
- DistSQL in-depth analysis: creating a dynamic distributed database
- Chapter 8 Intermediate Shell Tools II
- Go1.18升级功能 - 模糊测试Fuzz 从零开始Go语言
- 一文详解:SRv6 Policy模型、算路及引流
- 编码与进制
- It is enough for MySQL to have this article (disgusting typing 37k words, just for Bojun!!!)
- tcp协议传输中的粘包问题
猜你喜欢
MySQL 灵魂 16 问,你能撑到第几问?
The Road to Ad Monetization for Uni-app Mini Program Apps: Rewarded Video Ads
ML's shap: Based on FIFA 2018 Statistics (2018 Russia World Cup) team match star classification prediction data set using RF random forest + calculating SHAP value single-sample force map/dependency c
It is enough for MySQL to have this article (disgusting typing 37k words, just for Bojun!!!)
IDEA usage skills
【MySQL】DQL相关操作
The reason for not using bs4 is that the name is too long?Crawl lottery lottery information
Chapter 8 Intermediate Shell Tools II
ClickHouse 数据插入、更新与删除操作 SQL
Go1.18升级功能 - 模糊测试Fuzz 从零开始Go语言
随机推荐
DistSQL in-depth analysis: creating a dynamic distributed database
正则表达式语法及使用
MySQL 8.0.29 设置和修改默认密码
设备树的引入与体验
一文详解:SRv6 Policy模型、算路及引流
MySQL Soul 16 Questions, How Many Questions Can You Last?
When Navicat connects to MySQL, it pops up: 1045: Access denied for user 'root'@'localhost'
MySQL联合查询(多表查询)
CISP-PTE真题演示
【MySQL】DQL相关操作
网安学习-内网渗透3
cnpm的安装与使用
MySQL 游标
ClickHouse to create a database to create a table view dictionary SQL
CISP-PTE Zhenti Demonstration
DistSQL 深度解析:打造动态化的分布式数据库
MySQL连接时出现2003错误
成功解决ImportError: cannot import name ‘_validate_lengths‘
HCIP第十六天
Go的Gin框架学习