/* Create an abstract class, give this abstract class to someone else's class needs to inherit the abstract class * to obtain a three-dimensional bottom area, but we can not control how others are the bottom area. */package com.pan.e1;/** * * belongs to the night of the rain Spirit */public abstract class Gemetry {public abstract double Getarea ();}
- Because we want the code to implement various types of volume, we take the base area that needs to change frequently as an abstract class, and let it be the top-to-end type of its subclasses.
/** for the extraction class, the design of the main function *gemetry class and volume class is our own implementation, the purpose is to calculate the volume. */package com.pan.e1;/** * * belongs to the night of the Rain spirit */public class Volume { gemetry botton; Double height; Public Volume (Gemetry botton, double height) { This.botton=botton; this.height=height; } void Changebotton (Gemetry botton) { This.botton=botton; } Public double Getvolume () { return Botton.getarea () *height; }}
- The class of volume, but in this class we do not write the required object shape to die, but through the abstract class to achieve the bottom of the calculation, so that the various subclasses inherit abstract classes to achieve a variety of shapes
- Calculation of the volume. Make our code more robust and powerful.
/* * For us to achieve the area of the triangle, because the bottom surface is a quadrilateral, so we seek is prism volume */package com.pan.e1;/** * * belongs to the spirit of the night */public class Lader extends Gemetry {
double a,b,h; Public Lader (double a,double b,double h) { this.a=a; this.b=b; this.h=h; } Public double Getarea () { return (0.5* (a+b) *h);} }
- A subclass for which the bottom surface is trapezoidal
/* * A for us to achieve the area of the circle, because the bottom is a circle, so the demand for the cylinder volume */package com.pan.e1;/** * * belongs to the rain spirit of the night */public class Circle extends Gemetry { privat e Double R; Public Circle (Double r) { this.r=r; } Public double Getarea () { return (3.14*r*r); }}
- A sub-class with a rounded bottom face.
/* * Test the two classes we wrote: Gemetry and volume* Now if someone helps us achieve the bottom area of the cylinder, we can calculate the volume of the cylinder. */package com.pan.e1;/** * * author; the Night of the Rain spirit */public class Main {public static void Main (string[] args) { Volume v1;
gemetry G; G=new Lader (2,2,10); System.out.println ("trapezoid area" +g.getarea ()); V1=new Volume (g,58); System.out.println ("Volume of the column at the trapezoidal Bottom" +v1.getvolume ()); G=new Circle (2.0); System.out.println ("Area of the Circle" +g.getarea ()); V1=new Volume (g,4.0); System.out.println ("Volume of Cylinder" +v1.getvolume ());} }
By writing different classes to inherit the Gemetry abstract class, we can implement the volume of the non-dimensional shape, and our previous code does not need any changes.
By using the up-to-turn type, we can program for abstraction, focus on the logic design of the program upfront, and then inherit the abstract class or interface from the subclass
The implementation of specific functions, or sub-class can be written to other people to write, to achieve the effect of parallel development software, so as to improve the speed of software development, perhaps this is the team to develop software to take the method of it.
This is object-oriented programming!!!
Java-oriented abstract programming allows programmers to focus on the logical structure design of the program