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

了解Shader

腾讯云HiFlow场景连接器

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

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

Codeforces summer training weekly (7.14~7.20)

How to calculate the profit and loss of spot Silver

Leetcode 2351. the first letter that appears twice

普通设备能不能接入TSN时间敏感网络?

Introduction to QT drawing system

数仓搭建——DWS层
随机推荐
Advanced MySQL -- stored procedures and custom functions
Software testing interview question: where do your performance testing requirements come from?
Shutter -- password login registration interface
牛客多校第三场A,C+权值线段树
了解Shader
文章复现:超分辨率网络FSRCNN
Codeforces暑期训练周报(7.21~7.27)
Cesium add dynamic pop-up
Oracle RAC cluster file directory migration
彻底搞懂kubernetes调度框架与插件
Icml2022 | online decision transformer
Token is used in nodejs
C language main function transfer parameters
From functional testing to automated testing, my monthly salary has exceeded 30k+, and I have 6 years of testing experience.
Distributed | how to import data into dble quickly through the split function of dble
爬虫学习的一个综合案例——访问网站
软件测试面试题:think_time的作用是什么?
牛客网刷题训练(三)
String
PHP利用某些函数bypass waf探讨