Ninth interfaces and interface callback operations ---

A Title:

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

Second, the code and comments:

1. Square categories:

// * define a square Shape class to inherit an interface, which is a square side length and the area length method of seeking * @
 Package ccut.lyq;
 public  class zhengfangxing the implements Shape {
     Double length;
     Double Area; 
    zhengfangxing ( Double length) {
         the this = .length length; 
    } 
    public  Double the getArea () {
         return Area * length = length; 
    } 
}
 

2. triangle-like:

// * define a triangular shape class inherits its interfaces * triangle side length a, b, c, and the area of a triangular configuration method and a method of rewriting @
 Package ccut.lyq;
 public  class sanjiaoxing the implements the Shape {
     Double A;
     Double B;
     Double C; 
    
    sanjiaoxing ( 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 the Math.sqrt (D * (DA) * (DB) * (D-c));
    }
}

3. rectangle categories:

// * Defines a rectangular shape class inherits port junction, length of the rectangle is defined Chang, Kuan width, a rectangular area is calculated * @
 Package ccut.lyq;
 public  class Juxing the implements the Shape {
     Double Kuan;
     Double Chang; 
    Juxing ( Double Kuan, Double Chang) {
         the this .chang = Chang;
         the this .kuan = Kuan; 
    } 
    public  Double the getArea () {
         return Kuan * Chang; 
    } 
}

4. Trapezoid:

// * define a trapezoidal shape class that inherits the interface * @
 Package ccut.lyq;
 public  class Juxing the implements the Shape {
     Double Shangdi;
     Double xiadi;
     Double Gao; 
    
    Juxing ( Double Shangdi, Double xiadi, Double Gao) {
         the this .xiadi = xiadi ;
         the this .shangdi = Shangdi;
         the this .gao = Gao; 
    } 
    public  Double the getArea () {
         return (+ xiadi Shangdi) * Gao / 2 ; 
    } 
}

5. Round categories:

// * class inheritance circular shape defined interfaces, round edge length r, and a round configuration and a method of rewriting area * @
 Package ccut.lyq;
 public  class Circle the implements the Shape {
     Double R & lt; 
    Final Double the PI = 3.14 ; 
    Circle ( Double R & lt) {
         the this .r = R & lt; 
    } 
    public  Double the getArea () {
         return the PI * R & lt * R & lt; 
    } 
}

6 cylinder categories:

// * class definitions cylinder, defines a method of changing the bottom, High Definition cylinder, and pattern area * @
 Package ccut.lyq;
 public  class Cone { 
    the Shape Shape; 
    Double High;
     public Cone (the Shape Shape, Double High) {
         the this .shape = Shape;
         the this .high = High; 
    } 
    public  Double getVolume () {
         return shape.getArea () * High; 
    } 
    public  void setShape (the Shape Shape) {
         the this .shape = Shape; 
    } 
}

7. factory class:

// * create a factory class, object selection pattern shape of the bottom of the cylinder with the switch statement * is empty, and //
 Package ccut.lyq;
 Import Classes in java.util *. ;
 Public  class Factory's { 
    the Shape Shape = null ; 
    Scanner INPUT = new new Scanner (the System.in);
     public Factory's ( Double height) { 
        Cone Cone = new new Cone ( the this .getShape (), height); 
        System.out.println (cone.getVolume ()); 
    } 
    public the Shape getShape () { 
        System.out.println ( "Please select the bottom pattern: rectangular: R & lt; square: S; round: C; triangle: V; trapezoidal: T");
         Char A = input.next () the charAt (0. );
         Switch (A) {
         Case 'R & lt': System.out.println ( "rectangle in the bottom of cylinder volume:"); Shape = new new the Rectangle (3,6); BREAK ;
         Case 'S': System.out.println ( "squares in the bottom of the cylinder volume:"); Shape = new new square (. 5); BREAK ;
         Case 'C': the System. out.println ( "the bottom of a circular cylinder volume:"); Shape = new new circle (. 8); BREAK ;
         Case 'V': System.out.println ( "triangle in the bottom of the column volume : "); Shape = new new Triangle (that five, three); BREAK ;
         Case 'T': the System.out.println ( "the bottom of a ladder as the volume of a cylinder:"); shape =new Trapezoid(3,5,7);break;
        }
        return shape;
    }
}

8. graphics classes:

// * define a shape of the interface used for the column bottom * @
 Package ccut.lyq;
 public  interface the Shape {
     abstract  Double the getArea (); 
}

9. main categories:

//*主类*//
package ccut.lyq;
public class Text {
    public static void main(String[] args) {
        double height=6;
        Factory factory=new Factory(height);
    }
}

Third result:

Guess you like

Origin www.cnblogs.com/lyqqqq/p/11669059.html