9th jobs - Interface and callback interfaces

1. Title:

       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.

2. Code:

/ ** define an interface method abstract shape a request area, a conventional amount of PAI 
 * 
 * / 
Package homework1_1; 

public  interface the Shape {
     Double PAI = 3.14 ;
     public  Double the getArea (); 

}

 

/ ** define the class rectangle, with long rectangles represent chang representative kuan width of a rectangle, rectangular configuration; which contains a method to find the circumference zhouchang 
a getarea for mensuration 
 * 
 * / 
Package homework1_1; 

public  class rectangle the implements {the Shape
  Double Chang;
  Double Kuan; 
 Rectangle ( Double Chang, Double Kuan) {
      the this .chang = Chang;
      the this .kuan = Kuan; 
 } 
 public  Double zhouchang () {
      Double C = (Chang Kuan +) * 2 ;
      return C; 
 } 
 public  Double getArea(){
     return chang*kuan;
 }
}
/ ** define the subclass inherits the parent class Rectangle square, wherein the two methods, the representative getarea seeking area, zhouchang representatives find the circumference;
  * / 
Package homework1_1; 

public  class square the extends Rectangle { 
    square ( Double Side) {
         Super (Side, Side); 
    } 
    public  Double the getArea () {
         return Chang * Chang; 
    } 
    public  Double zhouchang () {
          Double C * = Chang. 4 ;
          return C; 
    } 
}
 
 

 

 
/ ** a triangle-like, has its own method of mensuration; 
 * 
 * / 
Package homework1_1; 

public  class Triangle the implements the Shape {
     Double A;
     Double B;
     Double C; 
    Triangle ( Double A, Double B, Double C) {
         the this II.A = A;
         the this .B = B;
         the this .c = C; 
    } 
    public  Double the getArea () {
         Double D = (A + B + C) / 2 ;
         return Math.sqrt(d*(d-a)*(d-b)*(d-c));
    }
}
/ ** bottom area of the cylinder defined by shape class and referenced own high, the method to calculate the volume getvolume, setarea change type; 
 * * 
 * / 
Package homework1_1; 

    public  class Cone { 
        the Shape shape; 
        Double height; 
        Cone (the Shape shape, Double height) {
             the this .shape = Shape;
             the this .height = height; 
        } 
     public  Double getVolume () {
          return shape.getArea () * height; 
         
     } 
     public  void setArea (the Shape Shape) {
          the this .shape = Shape; 
     } 
    }
/ ** Factory Functional character according to a user may pass what type of Shape; 
 * 
 * / 
Package homework1_1; 

public   class Factory {
     static the Shape Shape = null ; 
    
    public  static   the Shape whatShape ( char C) {
     Switch (C) {
     Case 'R & lt': Shape = new new Rectangle (5,3); BREAK ;
     Case 'T': Shape = new new Triangle (5,3,6); BREAK ;
     Case 'S': Shape = new new Square (. 5); BREAK ;
     Case 'C': Shape =new circle(5);break;
    }
     return shape;
}
}
/ * Define a main class T, get different depending on the character of a bottom uploaded to create an object given cylinder volume output parameter; 

 * 
 * / 
Package homework1_1; 

Import java.util.Scanner; 

public  class T { 

    / ** 
     * @ param args
      * / 
    public  static  void main (String [] args) {
         // the TODO Auto-Generated Method Stub 
        Scanner in = new new Scanner (the System.in); 
        System.out.println ( "enter character:" );
         char C in.next = () the charAt (0. ); 
        Cone Cone = new new Cone (factory.whatShape (C),. 5 );
    System.out.println(cone.getVolume());
    }
    
    }

3. Run Results:

Guess you like

Origin www.cnblogs.com/sdw98/p/11610846.html
9th