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

MySql基础知识汇总

【万字长文】使用 LSM-Tree 思想基于.Net 6.0 C# 实现 KV 数据库(案例版)

MySQL数据库的简单使用

Pyqt5 rapid development and practice 3.1 QT designer quick start

Generation and transformation of pulse waveform

JVM基本概念及内存管理模型
![36. [difference between const function and function]](/img/14/0f01fdace2ec17e873522cef4ac735.png)
36. [difference between const function and function]

DP的基本递归方程

MySQL deadlock analysis

Summary of common cmake commands
随机推荐
为什么放弃npm转向yarn了
找工作4个月,面试15家,终于拿到3个offer,定级P7+
ArrayList of novice source code
Basic use of logging
Real time streaming protocol --rtsp
Reproduce PHP one sentence Trojan horse
Simple use of MFC multithreading
There is an unhandled exception at 0x003b66c3 in MFC: 0xc000041d: unhandled exception encountered during user callback
MySQL learning notes
In depth interpretation of happens before principle
easyui05
梅科尔工作室-华为14天鸿蒙设备开发实战笔记八
Common library installation
Relationship between pixels and memory
Scrapy shell出现的一个错误
UDF and analysis case of sparksql, 220725,
雨课堂 《知识产权法》笔记
Caused by: scala.MatchError: None (of class scala.None$)
MySQL deadlock analysis
mysql数据库进阶