Java ninth job - Callback Interface

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.

 Source:

1.Shape.java

/ * Create a graphical interface that defines a method of seeking area * /

 

package cn.edu.ccut.jiekou;

public interface Shape {
    double getArea();

}

2.JuXing.java

/ ** Create a rectangle class, member variables defined method (length and width) and seeking area * /

package cn.edu.ccut.jiekou;

public class JuXing implements Shape{
    double length;
    double width;
    public JuXing(double length,double width){
        this.length=length;
        this.width=width;    
    }
    public double getArea(){
        return length*width;
    }
    
}

3.ZhengFangXing.java

/ ** Create a subclass inherits a square rectangle class, create a constructor argument and mensuration methods * /

package cn.edu.ccut.jiekou;

public class ZhengFangXing extends JuXing{
    public ZhengFangXing(double length){
        super(length,length);
    }
    
    public double getArea(){
        return length*length;
    }

}

4.SanJiaoXing.java

/ ** Create a triangle-like, defining the member variables ( A, B, C ), and find the area of methods * /

package cn.edu.ccut.jiekou;

public class SanJiaoXing implements Shape{
    double a;
    double b;
    double c;
    public SanJiaoXing(double a,double b,double c){
        this.a=a;
        this.b=b;
        this.c=c;
        
    }
    public double getArea(){
        double p=(a+b+c)/2;
        return Math.sqrt(p*(p-a)*(p-b)*(p-c));
    }
    
}

5.TiXing.java

/ ** Create a trapezoidal class member variables defined (on the bottom m, Lower n, higher H ) and a method of seeking area * /

package cn.edu.ccut.jiekou;

public class TiXing implements Shape{
    double m;
    double n;
    double h;
    public TiXing(double m,double n,double h){
        this.m=m;
        this.n=n;
        this.h=h;
    }
    public double getArea(){
        return (m+n)*h/2;
    }

}

6.Yuan.java

/ ** Create a circle class, member variables defined ( R & lt and PI and initializes PI = 3.14 ) and a method of seeking area * /

package cn.edu.ccut.jiekou;

public class Yuan implements Shape{
    double r;
    double PI=3.14;
    public  Yuan(double r){
        this.r=r;
    }
    public double getArea(){
        return PI*r*r;
    }

}

7.ZhuTi.java

/ ** Create a class column, and the high definition of the graphic objects, a method to calculate the volume defined * /

package cn.edu.ccut.jiekou;

public class ZhuTi {
    Shape g;
    double h;
    public ZhuTi(Shape g,double h){
        this.g=g;
        this.h=h;
        
    }
    public double getV(){
        return g.getArea()*h;
    }
    

}

8.Factory.java

/ ** Create a factory class that defines a graphics object G , to create a Shape type of method SS , a switch is defined in the method, returns the graphical object g * /

Package cn.edu.ccut.jiekou; 

public  class Factory's { 

        static the Shape G = null ;
         public  static the Shape SS ( char CH) {
         Switch (CH) {
         Case 'J': of System.out.print ( "rectangle as the base column volume: "); G = new new Juxing (6,8); BREAK ;
         Case 'Z': of System.out.print (" squares in the bottom of the cylinder volume: "); G = new new ZhengFangXing ( . 6); BREAK ;
         Case 'S': of System.out.print ( "triangle-bottom cylinder volume:"); G = new new SanJiaoXing (6,8,10); BREAK ;
         Case't': System.out.print ( "the bottom of a ladder as the volume of a cylinder:"); G = new new TiXing (5,7,6); BREAK ;
         Case 'Y': of System.out.print ( " a bottom circular cylinder volume: "); G = new new Yuan (. 9); BREAK ; 
        } 
        return G; 
        } 
        
}

9.Test.java

/ ** Create a Test class that defines the main method, the definition of a for loop, a column defines the object, call the method to calculate the volume, the output * /

Package cn.edu.ccut.jiekou; 

Import java.util.Scanner; 

public  class the Test {
     public  static  void main (String [] args) { 
    
        for ( int I = 0; I <. 5; I ++ ) { 
        Scanner Scanner = new new Scanner (System.in); 
        System.out.println ( "Please enter the graphics represent the character:" );
         char CH = Scanner.next () charAt (0. ); 
        
        ZhuTi zhuti = new new ZhuTi (Factory.ss (CH ),. 8 ); 
        System.out.println (zhuti.getV ()); 
        } 
    } 
    }

operation result:

 

Guess you like

Origin www.cnblogs.com/GXTSTAY/p/11616874.html