Ninth job --- callback interfaces

I, entitled

  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.

/ * Create a boundary interface Shape storage area for the method of calculating * * / 
Package COM;
 public  interface Shape {
 Double the getArea (); 
}
/ ** Create a rectangle class, to achieve the realization of the graphics interface, the method to calculate the area of the rewriting, and construction methods, length and width of the member variables * * / 

Package COM;
 public  class Rect the implements the Shape {
     Double WID, len;
     public Rect ( Double len , Double WID) {
         the this .wid = WID;
         the this .LEN = len; 
    } 
    
    public  Double the getArea () {
         return WID * len; 
    } 

}
/ ** Year boundary circle class, to achieve the realization of the graphics interface, the method to calculate the area of the rewriting, and construction methods, the member variable radius * * / 
Package COM;
 public  class Circle the implements the Shape {
     Double R & lt;
     public Circle ( Double R & lt) {
         the this .r = R & lt; 
    } 

    public  Double the getArea () {
         return Math.PI R & lt * * R & lt; 
    } 

}
/ ** Create a square-based, graphical interface to achieve the realization method of calculating the area, and construction methods overridden side member variables * * / 
Package COM;
 public  class Squre the implements the Shape {
     Double Bian; 
     Squre ( Double Bian) {
        the this . = Bian Bian;}
     public  Double the getArea () {
         return Bian * Bian; 
    } 
}
/ ** Create a trapezoidal class, to achieve the realization of graphics interface, the method to calculate the area of the rewriting, and construction methods, the member variable bottom, the lower bottom, high * * / 

Package COM;
 public  class Tixing the implements the Shape {
     Double Top, High, DI;
     public Tixing ( Double Top, Double High, Double DI) {
         the this .top = Top;
         the this .di = DI;
         the this .high = High; 
    } 
    public  Double the getArea () {
         return (Top + High) High * / 2 ; 
    } 
}
/ ** Create a triangle-like, to achieve the realization of graphics interface, the method to calculate the area of the rewriting, and construction methods, member variables trilateral long * * / 

Package COM;
 public  class Trangle the implements the Shape {
     Double A, B, C; 
    Trangle ( Double A, Double B, Double C) {
         the this II.A = A;
         the this .B = B;
         the this .c = C; 
    } 
    public  Double the getArea () {
         Double P = (A + B + C) / 2 ; 

        return the Math .sqrt (P * (PA) * (Pb) * (p- C)); 
    } 

}
/ ** Create a factory class, a character for input, and to achieve the appropriate volume * * / 
Package COM;
 Import the java.io.InputStreamReader;
 Import java.util.Scanner;
 public  class Fac { 
    the Shape Shape; 
    Scanner REA = new new Scanner ( the System.in); 
    
    char CH = rea.next () the charAt (0. ); 
    
    public the Shape getShape () {
         Switch (CH) {
         Case 'S': Shape = new new Trangle (3,4, 5); BREAK ;    
         Case 'C': Shape = new new Circle (. 3); BREAK ;
         Case 'r': shape = new Rect(3,4); break;
        case 'z': shape = new Squre(4); break;
        case 't': shape = new Tixing(3,4,4); break;
        
        }
        return shape;
    }

    public char getCh() {
        return ch;
    }

    public void setShape(Shape shape) {
        this.shape = shape;
    }

    public void setCh(char ch) {
        this.ch = ch;
    }



}
/ ** Create a cartridge type, and the post have class constructor, a method of calculating the volume of write, seek class method calls the graphics area, and a method of soled * * / 
Package COM;
 public  class Cone { 
     the Shape Shape; 
     Double High; 
     Cone (the Shape Shape, Double High) {
          the this .shape = Shape;
          the this .high = High; 
     } 
    public  Double getVolume () {
         return shape.getArea () * High; 
    } 


void   changeDi (the Shape Shape) {
 the this . = Shape Shape;        
   } 
}
 
 

 

 
/ ** Create a main method, and examples of chemical classes cartridge type, the output volume * * / 
Package COM;
 Import java.util.Scanner;
 public  class the Text {
     public  static  void main (String [] args) {
         for ( int I 0 =; I <. 5; I ++ ) { 
        of System.out.print ( "enter shape" ); 
        Fac FAC1 = new new Fac (); 
        Cone cone1 = new new Cone (fac1.getShape (),. 5 ); 
        cone1.changeDi ( fac1.getShape ()); 
        Double vol1 = cone1.getVolume (); 
        System.out.println ( "volume" +Vol1); 
        } 
    } 
}

 

Guess you like

Origin www.cnblogs.com/chenxiangyuu/p/11612087.html