当前位置:网站首页>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 .
边栏推荐
- The understanding of domain adaptation in transfer learning and the introduction of three technologies
- Kibana6.2.4 version update x-pack certification
- Brushes and brushes
- Article reproduction: super resolution network fsrcnn
- 面试题 01.05. 一次编辑
- Storage practices for high-performance computing scenarios, see here
- Custom events
- LeetCode 2347. 最好的扑克手牌
- Cross domain requests in nodejs
- 面试题 01.06. 字符串压缩
猜你喜欢

Introduction to QT drawing system

了解Shader

Harmonyos 3 was officially released: Hongmeng mobile phones are smooth and safe, and Hongmeng terminals are often used

JUC concurrent programming learning

I want to get 20K after 3 years of experience, but I haven't got it for half a month?

“你“想当测试/开发程序员吗?努力发芽的我们......

HarmonyOS 3正式发布:鸿蒙手机流畅安全,鸿蒙终端常用常新

Leetcode 2351. the first letter that appears twice

Codeforces暑期训练周报(7.21~7.27)

Qlib tutorial - based on source code (II) local data saving and loading
随机推荐
Rviz uses arbotix to control robot motion
8000 word explanation of OBSA principle and application practice
docker 本地搭建mysql主从
26.抽象化和模板思想
Article reproduction: super resolution network fsrcnn
使用Gateway的流式api修改请求路径
Content bypass sharing
Lua快速上手
Can ordinary equipment access TSN time sensitive network?
Day 013 一维数组练习
Briefly understand namenode and datanode
Software testing interview question: where do your performance testing requirements come from?
普通设备能不能接入TSN时间敏感网络?
C language main function transfer parameters
Count the number of given strings in a string
LeetCode 2347. 最好的扑克手牌
现货白银如何计算盈亏
Day 013 one dimensional array exercise
软件测试面试题:你们的性能测试需求哪里来?
数仓搭建——DWS层