第9次作业--接口及接口回调
柱体类
/**创建一个柱体类Cone,定义柱体的高、构造方法、换底方法和求体积方法*/
1 package package1; 2 3 4 public class Cone { 5 Shape shape; 6 double high; 7 8 public Cone(Shape shape,double high){ 9 this.shape=shape; 10 this.high=high; 11 } 12 13 public double getVolume(){ 14 return shape.getArea()*high; 15 } 16 public void setShape(Shape shape){ 17 this.shape=shape; 18 } 19 }
矩形类
/** 创建一个矩形类Rectangle,继承shape接口,定义长和宽,定义构造方法和重写求面积方法*/
1 package package1; 2 3 public class Rectangle implements Shape { 4 protected double width; 5 protected double length; 6 public Rectangle(double width,double length){ 7 this.width=width; 8 this.length=length; 9 } 10 Rectangle() { 11 12 } 13 public double getArea(){ 14 System.out.println("r"); 15 return width*length; 16 } 17 18 }
正方形类
/*创建一个正方形类Zheng 继承接口Shape,定义边长、构造方法、求面积方法*/
1 package package1; 2 3 public class Zheng implements Shape{ 4 double side; 5 public Zheng(double side) { 6 this.side =side; 7 } 8 public double getArea(){ 9 return side*side; 10 } 11 }
三角形类
/*创建一个三角形类Triangle继承shape接口,定义三边a,b,c,定义构造方法、求面积方法*/
1 package package1; 2 3 public class Triangle implements Shape { 4 double a; 5 double b; 6 double c; 7 8 public Triangle(double a,double b,double c){ 9 this.a=a; 10 this.b=b; 11 this.c=c; 12 } 13 public double getArea() { 14 System.out.println("t"); 15 double p =(a+b+c)/2; 16 return Math.sqrt(p*(p-a)*(p-b)*(p-c)); 17 18 } 19 }
梯形类
/*创建一个梯型类,继承Shape接口,定义上底、下底和高、定义构造方法、定义求面积方法*/
1 package package1; 2 3 public class Tixing implements Shape{ 4 double a; 5 double b; 6 double high; 7 Tixing(double a,double b,double high){ 8 this.a=a; 9 this.b=b; 10 this.high=high; 11 } 12 13 public double getArea(){ 14 return (a+b)*high/2; 15 } 16 }
圆形类
/*创建一个圆形类Circ继承接口Shape,定义半径r,常量PI,定义构造方法和求面积方法*/
1 package package1; 2 3 public class Circ implements Shape{ 4 double r; 5 final double PI=3.14; 6 public Circ(double r) { 7 this.r=r; 8 } 9 public double getArea() { 10 return r*r*PI; 11 12 } 13 14 }
工厂类
/*创建一个工厂类Factory,声明Shape对象为空,引用输入操作提示用户输入字符,利用switch语句给柱体赋底,数据回调给接口Shape*/
1 package package1; 2 3 import java.util.Scanner; 4 5 public class Factory { 6 7 Shape shape = null; 8 Scanner reader = new Scanner(System.in); 9 10 public Shape getShape(){ 11 char i =reader.next().charAt(0); 12 switch (i) { 13 case 'j':System.out.println("以矩形为底的柱体体积是:");shape=new Rectangle(2, 3); break; 14 case 'z':System.out.println("以正方形为底的柱体体积是:");shape=new Zheng(4); break; 15 case 's':System.out.println("以三角形为底的柱体体积是:");shape=new Triangle(2, 3, 4); break; 16 case 't':System.out.println("以梯形为底的柱体体积是:");shape=new Tixing(2, 3, 4); break; 17 case 'y':System.out.println("以圆形为底的柱体体积是:");shape=new Circ(5); break; 18 } 19 return shape; 20 } 21 }
接口
/*定义接口Shape,抽象方法getArea*/
1 package package1; 2 3 public interface Shape { 4 abstract double getArea(); 5 }
主类
/*定义变量high作为主体的高,通过Scanner方法输入高。用for循环输出五个图形为底的体积,创建一个工厂类对象,一个柱体类对象,引用换底方法,输出柱体的体积*/
1 package package1; 2 import java.util.Scanner; 3 4 public class Test { 5 6 7 public static void main(String[] args) { 8 System.out.println("请输入柱体的高:"); 9 double high; 10 Scanner reader =new Scanner(System.in); 11 high=reader.nextDouble(); 12 for(int i=0;i<5;i++){ 13 System.out.println("请选择柱体的底面图形,j为矩形、z为正方形、s为三角形、t为梯形、y为圆形");
14 Factory factory = new Factory(); 15 Cone cone = new Cone(factory.getShape(), high); 16 cone.setShape(factory.shape); 17 System.out.println(cone.getVolume()); 18 } 19 } 20 }
运行结果