当前位置:网站首页>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 .
边栏推荐
猜你喜欢

Qlib教程——基于源码(二)本地数据保存与加载

26.抽象化和模板思想

Storage practices for high-performance computing scenarios, see here

Codeforces summer training weekly (7.21~7.27)

迅为i.MX6ULL开发板Qt系统移植-交叉编译Qt代码

ICML2022 | 在线决策Transformer

Can anime characters become "real people"? Paddegan helps you find the TA of "tear man"

My rich second generation friend

Docker builds MySQL master-slave locally

Harmonyos 3 was officially released: Hongmeng mobile phones are smooth and safe, and Hongmeng terminals are often used
随机推荐
Kibana6.2.4 version update x-pack certification
3000 words and 11 pictures hard core popular science: what is edge computing? What are the connections and differences with cloud computing?
Cesium add light sweep
Docker builds MySQL master-slave locally
股票问题5连
Can anime characters become "real people"? Paddegan helps you find the TA of "tear man"
Distributed | how to import data into dble quickly through the split function of dble
2.2综合应用题-顺序表
Thoroughly understand kubernetes scheduling framework and plug-ins
Day 013 one dimensional array exercise
VLAN实验
Brushes and brushes
My rich second generation friend
软件测试面试题:性能测试指标有哪些?
Neuron 2.1.0 release: it supports sparkplug B specification and more complete industrial protocol support
Briefly understand namenode and datanode
Develop plug-ins for the recording function of flutter
Software testing interview question: where do your performance testing requirements come from?
Baidu PaddlePaddle easydl: when AI enters the factory, "small bearing" can also turn "big industry"
现货白银如何计算盈亏