当前位置:网站首页>Understanding of Abstract Programming
Understanding of Abstract Programming
2022-07-26 11:30:00 【Baby paper d】
| What is abstract oriented programming , I didn't pay much attention to it the first time , Feeling is not very important , But when I look closely , Abstract oriented functions are so powerful , I should sum it up again . |
Abstract oriented programming , seeing the name of a thing one thinks of its function , Is for abstract classes (abstract) Programming for a series of operations . in other words When designing some important class , Don't make the class specific , It's for abstract classes , namely Important data in design class yes Objects of abstract classes , Not an object declared by a concrete class .
Just to make it simple , Is to define a abstract class and One abstract Method , Define one or more abstract Subclasses of classes , Override... In a subclass abstract Class abstract Method ( Methods overridden in subclasses are no longer abstract Method ), Then you can declare only one abstract class , recycling On the transformation object Call the method it overrides . In this way, a declared variable can cope with many different changes .
Let's take a look at the application of an example :( Find the volume of the cylinder )
abstract Classes and subclasses :
public abstract class Geometry {
public abstract double getArea();// Just a simple structure abstract Method , Don't add anything .
}class Circle extends Geometry{
double r;
Circle(double r){// Constructors
this.r = r;
}
public double getArea() { / / Method rewrite
return r * r * 3.14;
}
}
class Rectangle extends Geometry{
double a,b;
Rectangle(double a,double b){
this.a = a;
this.b = b;
}
public double getArea() {
return a*b;
}
}
Get the class of column volume , No longer rely on concrete classes , But to Geometry class :
public class Pillar {
Geometry bottom;// Statement Geometry object ( Abstract objects )
double hight;
Pillar(Geometry bottom,double h){// Constructors , Get value
this.bottom = bottom;
this.hight = h;
}
public double getVolume(){// Return volume value
if(bottom == null){
System.out.println(" No bottom , Unable to calculate area ");
return 0;
}
return bottom.getArea()*hight;// The call here is getArea() The function is based on bottom To determine the transformation object .
}
}
Create the upper transformation object , Determine its calling function , Output its value
public class Application {
public static void main(String[] args) {
Pillar pillar;
Geometry bottom;
bottom = null;
pillar = new Pillar(bottom,5);
System.out.println(" Volume :" + pillar.getVolume());
bottom = new Circle(2);// On the transformation object
pillar = new Pillar(bottom,1);
System.out.println(" Volume :" + pillar.getVolume());// It's called Circle Inside getVolume().
bottom = new Rectangle(5,2);// On the transformation object
pillar = new Pillar(bottom,1);
System.out.println(" Volume :" + pillar.getVolume());// It's called Rectangle Inside getVolume().
}
}
The purpose of abstract oriented programming is to cope with the changes of user needs , Separate the code in a class that often needs to be changed due to changes in requirements .
The core of abstract oriented programming is to assign each possible change in the class to a subclass of the abstract class , So that the designer of the class no longer cares about the specific implementation .
边栏推荐
- Postman export import
- leetcode-209. 长度最小的子数组(二分、前缀和、滑动窗口)
- Leetcode-209. subarray with the smallest length (binary, prefix and, sliding window)
- Pytest case execution sequence
- 外包干了四年,废了...
- [报错]Exception: Found duplicate column(s) in the data schema: `value`;
- How the ThreadPoolExecutor performs tasks
- [vscode] how to connect to the server remotely
- The company cannot access station B
- UDF and analysis case of sparksql, 220725,
猜你喜欢

3dunity game project practice - first person shooting game

Pytest execution rules_ Basic usage_ Common plug-ins_ Common assertions_ Common parameters

C#笔记

实时流式协议--RTSP

数据中台建设(二):数据中台简单介绍

349. Intersection of two arrays

A new technical director asked me to do an IP territorial function~

easyui05

easyui03

Le audio specification overview
随机推荐
3Dunity游戏项目实战——第一人称射击游戏
C notes
Several ways of communication between threads
十年架构五年生活-06 离职的冲动
并发三大性质
How the ThreadPoolExecutor performs tasks
Harbor2.2 用户角色权限速查
一步一步入门使用g2o解决ICP问题-估计有匹配关系的两组3维点集之间的变换关系
QT - connect USB camera
There is an unhandled exception at 0x003b66c3 in MFC: 0xc000041d: unhandled exception encountered during user callback
BLE之ATT请求
静态路由和动态路由
AuthorizingRealm简介说明
五万美元的年薪是如何花光的
Novice source code hashtable
MySQL learning notes
公司无法访问b站
C#笔记
The combination of pytest confitest.py and fixture
Caused by: scala. MatchError: None (of class scala.None$)