当前位置:网站首页>26.抽象化和模板思想
26.抽象化和模板思想
2022-07-27 23:41:00 【denggongming】
目录
一、抽象化
一、概念
被abstract关键字修饰的类叫做抽象类。当在继承关系中设计父类的时候,如果父类中的一个公共方法父类不确定该方法的具体实现,那么父类就干脆不实现,将其定义成抽象方法,让子类自己去实现,将其具体化。
二、意义
1、因为抽象类不能实例化对象,所以必须要有子类来实现它之后才能使用。这样就可以把一些具有相同属性和方法的组件进行抽象,这样更有利于代码和程序的维护。
2、当又有一个具有相似的组件产生时,只需要实现该抽象类就可以获得该抽象类的那些属性和方法。
所以,Java中抽象类对于代码的维护和重用有很好的帮助,也是Java面向对象的一个重要体现。
三、特点
1、抽象父类无法直接创建对象,如果创建,编译无法通过而报错。只能创建其非抽象子类的对象。
2、抽象类中,可以有构造方法,是子类创建对象时,初始化父类成员使用的。
3、抽象类中不一定包含抽象方法,但有抽象方法的一定是抽象类,抽象类需要加上abstract。
4、抽象类的子类,必须重写抽象父类中所有的抽象方法,否则子类也必须定义成抽象类,编译无法通过而报错。
5.抽象类存在的意义是为了被子类继承,抽象类体现的是模板思想。
四、例子
假如我现在需要定义新司机和老司机类,新司机和老司机都有开车功能,开车的步骤都一样,只是驾驶时的姿势有点不同,新司机:开门,点火,双手紧握方向盘,刹车,熄火;老司机:开门,点火,右手握方向盘左手抽烟,刹车,熄火。我们可以将固定流程写到父类中,不同的地方就定义成抽象方法,让不同的子类去重写,代码如下:
分析:这里新司机和老司机开车流程不一样的地方只有抓方向盘的姿势,所以这里我们可以将这个步骤进行抽象化,将其定义为一个姿势方法,然后用子类来重写,每种姿势写进去。




二、模板思想
1、概念
把抽象类整体就可以看成是一个模板,模板中不能决定的东西定义成抽象方法,让使用模板的类(继承抽象类的子类)去重写抽象方法实现需求。
2、特点
1、子类按需重写父类中抽象化的方法。
2、以抽象父类中的定义进行调用。
3、目的
定义一个操作中的算法的骨架,而将一些步骤延迟到子类中。模板方法使得子类可以不改变一个算法的结构即可重定义该算法的某些特定步骤。
4、优点
1、封装不变部分,扩展可变部分。
2、提取公共代码,便于维护。
3、行为由父类控制,子类实现。
5、缺点
每一个不同的实现都需要一个子类来实现,导致类的个数增加,使得系统更加庞大。
边栏推荐
- Leetcode 2351. the first letter that appears twice
- Cesium add dynamic pop-up
- Software test interview question: how to find problems related to the database?
- Matlab 44 animation gradient drawing programs
- MATLAB 44种动漫渐变色绘图程序
- 伦敦银开盘时间知多少
- C language main function transfer parameters
- ICML2022 | 在线决策Transformer
- I want to get 20K after 3 years of experience, but I haven't got it for half a month?
- 彻底搞懂kubernetes调度框架与插件
猜你喜欢

Sort out SQL performance optimization and collect classics!

Fluent call interface UI

画刷和画笔

Lua advanced

Leetcode 2341. How many pairs can an array form

“蔚来杯“2022牛客暑期多校训练营3 补题题解(A、C、J)

内容bypass分享

Codeforces summer training weekly (7.14~7.20)

How the test architects of bat factories interpret various disputes of the test platform

If you are still using WiFi, you will be out: li-fi is better!!!
随机推荐
Wu xiongang sent an internal letter: arm's allegations are unwarranted, and no damage is allowed to the existing achievements!
Unknown database ‘xxxxx‘
Neuron 2.1.0 release: it supports sparkplug B specification and more complete industrial protocol support
JG-数据重置(wd)
Insider of container network hard core technology (7) sailing on the sea depends on the helmsman
使用Gateway的流式api修改请求路径
20 bad habits of bad programmers
Cap principle of [distributed development]
If you are still using WiFi, you will be out: li-fi is better!!!
BSP视频教程第21期:轻松一键实现串口DMA不定长收发,支持裸机和RTOS,含MDK和IAR两种玩法,比STM32CubeMX还方便(2022-07-24)
Redefine analysis - release of eventbridge real-time event analysis platform
Leetcode 2347. the best poker hand
自定义事件
Dart code comments and documentation specification
Principle of logistic regression
Cross domain requests in nodejs
[style set 1] tab
Harmonyos 3 was officially released: Hongmeng mobile phones are smooth and safe, and Hongmeng terminals are often used
Byte monthly salary 28K, share a wave of my automation testing experience
Lua get started quickly