当前位置:网站首页>26. Abstraction and template ideas
26. Abstraction and template ideas
2022-07-28 01:43:00 【denggongming】
Catalog
One 、 Abstraction
One 、 Concept
By abstract Keyword decorated class is called abstract class . When designing a parent class in an inheritance relationship , If a public method in the parent class, the parent class is not sure about the specific implementation of the method , Then the parent class simply does not implement , Define it as an abstract method , Let subclasses implement themselves , Make it concrete .
Two 、 significance
1、 Because abstract classes cannot instantiate objects , So you must have subclasses to implement it before you can use . In this way, some components with the same properties and methods can be abstracted , This is more conducive to the maintenance of code and program .
2、 When another component with similar characteristics is generated , You only need to implement the abstract class to get those properties and methods of the abstract class .
therefore ,Java Abstract classes in are very helpful for code maintenance and reuse , It's also Java An important embodiment of object-oriented .
3、 ... and 、 characteristic
1、 An abstract parent class cannot directly create an object , If you create , Compile failed and error reported . Only objects with non Abstract subclasses can be created .
2、 In an abstract class , There can be construction methods , Is when subclasses create objects , Initializes the .
3、 Abstract classes don't necessarily contain abstract methods , But it must be an abstract class that has abstract methods , Abstract classes need to be added abstract.
4、 A subclass of an abstract class , All abstract methods in the abstract parent class must be overridden , Otherwise, subclasses must also be defined as abstract classes , Compile failed and error reported .
5. Abstract classes exist in order to be inherited by subclasses , Abstract classes embody the idea of templates .
Four 、 Example
If I need to define new drivers and old drivers now , Both new and old drivers have the function of driving , The steps of driving are the same , It's just that the driving posture is a little different , New driver : Open door , ignition , Hold the steering wheel with both hands , brake , Stalling ; old hand : Open door , ignition , Hold the steering wheel with your right hand and smoke with your left hand , brake , Stalling . We can write the fixed process to the parent class , Different places are defined as abstract methods , Let different subclasses override , The code is as follows :
analysis : The only difference between the driving process of new drivers and old drivers here is the posture of grasping the steering wheel , So here we can abstract this step , Define it as a pose method , Then override with Subclasses , Write in every posture .




Two 、 Template thought
1、 Concept
The abstract class as a whole can be regarded as a template , What cannot be determined in a template is defined as an abstract method , Let the classes that use templates ( Inheriting subclasses of abstract classes ) To rewrite abstract methods to implement requirements .
2、 characteristic
1、 Subclasses override abstracted methods in the parent class on demand .
2、 Call with the definition in the abstract parent class .
3、 Purpose
Defines the skeleton of an algorithm in an operation , Instead, defer some steps to subclasses . The template approach allows subclasses to redefine certain steps of an algorithm without changing its structure .
4、 advantage
1、 Encapsulate the invariant part , Extend the variable part .
2、 Extract public code , Easy to maintain .
3、 Behavior is controlled by the parent class , Subclass implementation .
5、 shortcoming
Each different implementation needs a subclass to implement it , Causes the number of classes to increase , Make the system bigger .
边栏推荐
- Principle of logistic regression
- Opengauss active / standby architecture works with keeplive
- Learn how Baidu PaddlePaddle easydl realizes automatic animal recognition in aquarium
- 华为“天才少年”稚晖君又出新作,从零开始造“客制化”智能键盘
- HarmonyOS 3正式发布:鸿蒙手机流畅安全,鸿蒙终端常用常新
- 伦敦银开盘时间知多少
- Codeforces暑期训练周报(7.21~7.27)
- Can anime characters become "real people"? Paddegan helps you find the TA of "tear man"
- Cesium add inundation analysis measurement area
- 路由策略简介
猜你喜欢
随机推荐
C#学习笔记----C#连接MySQL数据库
Leetcode 2347. the best poker hand
数仓搭建——DWS层
【向 Dice Roller 应用添加图片】
自定义事件
【样式集合1】tab 栏
Can ordinary equipment access TSN time sensitive network?
Interview question 01.07. rotation matrix
Opengauss active / standby architecture works with keeplive
面试题 01.07. 旋转矩阵
面试题 01.05. 一次编辑
"Wei Lai Cup" 2022 Niuke summer multi school training camp 3 supplementary problem solution (a, C, J)
Develop plug-ins for the recording function of flutter
内容bypass分享
华为“天才少年”稚晖君又出新作,从零开始造“客制化”智能键盘
For newly installed PIP3, use no module named 'LSB_ Release 'problem
What is the opening time of London Silver
Summary of common shortcut keys in idea
Baidu PaddlePaddle easydl: when AI enters the factory, "small bearing" can also turn "big industry"
Day 013 一维数组练习









