当前位置:网站首页>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 .
边栏推荐
猜你喜欢
随机推荐
[learning progress] may
Pytest case execution sequence
二分模板总结
一步一步入门使用g2o解决ICP问题-估计有匹配关系的两组3维点集之间的变换关系
344. Reverse string
QT——连接USB摄像头
Real time streaming protocol --rtsp
Classic Bluetooth connection process
1837. Sum of digits under k-ary representation
After 4 months of job search and 15 interviews, I finally got 3 offers, ranking P7+
Cmake常用命令总结
MySQL transaction details
DP的基本递归方程
MLX90640 红外热成像仪测温传感器模块开发笔记(六)
ESP8266-Arduino编程实例-GPIO输入和输出
QT - connect USB camera
配置文件以rc结尾什么意思
想让照片中的云飘起来?视频编辑服务一键动效3步就能实现
Toolstrip border removal
Le audio specification overview

![[vscode] how to connect to the server remotely](/img/b4/9a80ad995bd589596d8b064215b55a.png)





![[development tool] ieda red](/img/2d/eec1f74c33ff21ae4951eae44b9369.png)
