Java interfaces and interface callback _Chris

topic:

  Callback interface and its use, simple factory mode, when inputting different characters indicate corresponding pattern obtained by using the graphical object factory class, then the figure is calculated in the column bottom volume.

Code:

1、Test.java

1  / * 
2  * Test class, the method comprising a master;
 3  * calling method to calculate the volume output;
 . 4   * / 
. 5  Package cn.edu.ccut.object3;
 . 6  
. 7  Import java.util.Scanner;
 . 8  public  class the Test {
 . 9  
10      public  static  void main (String [] args) {
 . 11          char C;
 12 is          the while ( to true ) {
 13 is              System.out.println ( "Please enter the bottom of column shape ( 'j' rectangles, 's' triangle,' z 'square,' t 'ladder,' y 'round,' F 'end of the process): " );
 14              Scanner Reader = new new Scanner (the System.
in);
15              C = reader.next () the charAt (0);. // C accepting character input; 
16              IF (C == 'F.') {      // Exit the program flag; 
. 17                  System.out.println ( "End of program! " );
 18 is                  BREAK ;
 . 19              }
 20 is              Factory F = new new Factory ();
 21 is              Cone Cone = new new Cone (f.ctrl_di (C), 5); // define rectangular objects, according to a user the end of output control, height 5; 
22 is              System.out.println ( "cylinder volume is:" + cone.getV ());
 23 is          }
 24      }
 25  
26 is }

2、factory.java

1  / * 
2  * factory class, simple factory pattern, a method comprising;
 . 3  * ctrl_di achieved based on user input transducer substrate;
 . 4   * / 
. 5  Package cn.edu.ccut.object3;
 . 6  
. 7  public  class Factory {
 . 8      the Shape = Shape null ;
 . 9      public the Shape ctrl_di ( char C) { // implements based on user input soled; 
10          Switch (C) {
 . 11          Case 'J': Shape = new new Rec (3,4-); BREAK ;
 12 is          Case ' S ': Shape = new new Triangle (3,4, 5); BREAK ;
13         case 'z':shape = new Zheng(2);break;
14         case 't':shape = new Trapezoid(4,6,7);break;
15         case 'y':shape = new Circle(3);break;
16         default:System.out.println("error");        
17         }
18         return shape;
19     }
20 }

3、Shape.java

1  / * 
2  * graphics interface;
 3  * defines an abstract method to calculate the area of the getArea;
 . 4   * / 
. 5  Package cn.edu.ccut.object3;
 . 6  
. 7  public  interface   the Shape {
 . 8      public  Double the getArea (); // Abstract The method of calculating the area; 
9 }

4、Rec.java

1  / * 
2  * rectangle class, using a graphics interface, a two member variables comprising constructor, and overrides the method getArea;
 3  * two member variables representing the length and width of holding;
 4  * constructor initialization, getArea calculate the area;
 . 5   * / 
. 6  Package cn.edu.ccut.object3;
 . 7  
. 8  public  class Rec the implements the Shape {
 . 9      Double A;
 10      Double B;
 . 11      Rec ( Double A, Double B) {
 12 is          the this II.A = A;
 13 is          the this .B = B;
 14      }
 15      public double getArea(){
16         return (a*b);
17     }
18 }

5, Zheng.java

1  / * 
2  * class square, rectangular inherited class, contains two methods;
 3  * constructor initialization, calculate the area of the getArea
 . 4   * / 
. 5  Package cn.edu.ccut.object3;
 . 6  
. 7  public  class Zheng the extends Rec {
 . 8      zheng ( Double A) {
 . 9          super (A, A); // use super call the parent class constructor; 
10      }
 . 11  
12 is      public  Double the getArea () { // The method of rewriting; 
13 is          return (A * A);
 14      }
 15 }

6、Circle.java

1  / * 
2  * circle class, using a graphical interface contains a member variable, two methods;
 3  * member variable radius r;
 4  * constructor initializes a getArea override method;
 . 5   * / 
. 6  Package CN. edu.ccut.object3;
 . 7  
. 8  public  class Circle the implements the Shape {
 . 9      Double R & lt;
 10      
. 11      public Circle ( Double R & lt) {
 12 is          the this .r = R & lt;
 13 is      }
 14      public  Double the getArea () {
 15          return (* Math.PI * R & lt R & lt);
 16      }
17 }

7、Triangle.java

1  / * 
2  * triangle class, using a graphical interface that includes three member variables, two methods;
 3  * member variables a, b, c represent the three sides of the triangle;
 4  * constructor initialization, overwriting the method of getArea;
 . 5   * / 
. 6  Package cn.edu.ccut.object3;
 . 7  
. 8  public  class Triangle the implements the Shape {
 . 9      Double a;
 10      Double B;
 . 11      Double C;
 12 is      
13 is      public Triangle ( Double a, Double B, Double C ) {
 14          the this II.A = A;
 15         this.b = b;
16         this.c = c;
17     }
18     public double getArea(){
19         double p = (a + b + c) / 2;
20         return Math.sqrt(p * (p - a) * (p - b) * (p - c));
21     }
22 }

8、Trapezoid.java

1  / * 
2  * Trapezoid, using a graphical interface member comprising three variables, two methods;
 3  * member variables a, b representative of trapezoid upper base and a lower bottom, high Representative High;
 . 4  * constructor, heavy write the getArea;
 . 5   * / 
. 6  Package cn.edu.ccut.object3;
 . 7  
. 8  public  class Trapezoid the implements the Shape {
 . 9      Double A;
 10      Double B;
 . 11      Double High;
 12 is      
13 is      public Trapezoid ( Double A, Double B, Double High) {
 14          the this II.A = A;
 15         this.b = b;
16         this.high = high;        
17     }
18     public double getArea(){
19         return (((a + b) * high) / 2);
20     }
21 }

9、Cone.java

1  / * 
2  * cylinder type, two member variables, three methods;
 3  * representative of the end member variable shape, representing a high High;
 4  * constructor initialization, the cylinder volume calculation getv, setDi can be used to change the bottom;
 5   * / 
. 6  Package cn.edu.ccut.object3;
 . 7  
. 8  public  class Cone {
 . 9      the Shape Shape;
 10      Double High;
 . 11      public Cone (the Shape Shape, Double High) {
 12 is          the this .shape = Shape;
 13 is          the this .high = High ;
 14      }
 15      public  Double getv () {
 16         return (shape.getArea()*high);
17     }
18     public void setDi(Shape shape){ //利用修改器换底
19         this.shape = shape;
20     }
21 }

operation result:

 

 

 

 

Guess you like

Origin www.cnblogs.com/chris-wang/p/11620316.html