zoukankan      html  css  js  c++  java
  • 第9次作业--接口及接口回调

    题目:

           利用接口和接口回调,实现简单工厂模式,当输入不同的字符,代表相应图形时,利用工厂类获得图形对象,再计算以该图形为底的柱体体积。

    1.代码实现:

    Test.java

    /**
     * 1.主类Test,主方法main,while循环,if else控制语句,字符变量x
     * 2.变量x用来接收用户输入的字符,if 执行退出, else 创建工厂对象,柱体对象,输出体积
     * 3.if 通过break退出循环,结束程序,else 调用fy.getShape(x)方法,给柱体对象传新参
     * */
    package cn.edu.ccut.po;
    
    import java.util.Scanner;
    
    public class Test {
    
        public static void main(String[] args) {
            while(true) {//while 无限循环
                
                System.out.println("请输入你需要的图形对应的字符:矩形(j)正方形(z)三角形(s)圆形(y)梯形(t),退出输入:B!");
    
                Scanner in = new Scanner(System.in);
                char x = in.next().charAt(0);//接收用户输入的字符 
                if(x=='B') {//退出程序条件
                    System.out.println("已退出程序!");
                    break;
                }
                else {
                    Factory fy = new Factory();//创建 工厂的对象
                    Zhuti zt = new Zhuti(fy.getShape(x),fy.h);//创建柱体对象zt,调用getShape方法得到新的shape和h的值
                    System.out.println(zt.getVolume());//输出主体的体积
                }
                
            }
    
        }
    
    }

    Factory.java  工厂模式

    /**
     * 1.一个主类Factory,4个成员变量,一个方法getShape,switch控制语句
     * 2.Factory工厂类用来封装方法,成员变量分别用来存储用户输入的值,局部变量shape用来获取新的图形
     * 3.通过方法里面的参数判断进入switch哪条语句,然后执行赋值语句,给shape赋新值,
     *           跳出循环返回shape值
     * */
    package cn.edu.ccut.po;
    
    import java.util.Scanner;
    
    public class Factory {//工厂类
        double a,b,c,h;
        Shape getShape(char x) {
            Shape shape=null;//接口变量
            Scanner in = new Scanner(System.in);
            switch(x){
                case 'j':
                    System.out.print("请分别输入矩形的宽、长、柱体的高:");
                    a = in.nextDouble();//读入宽
                    b = in.nextDouble();//读入长
                    h = in.nextDouble();//读入高
                    shape = new Rectangle(a,b);//接口回调
                    System.out.print("矩形柱体的体积为:");
                    break;
                case 'z':
                    System.out.print("请分别输入正方形的边长、柱体的高:");
                    a = in.nextDouble();//读入边长
                    h = in.nextDouble();//读入高
                    shape = new Zfx(a);//接口回调
                    System.out.print("正方形柱体的体积为:");
                    break;
                case 's':
                    System.out.print("请分别输入三角形的三边长、柱体的高:");
                    a = in.nextDouble();//读入边长
                    b = in.nextDouble();//读入边长
                    c = in.nextDouble();//读入边长
                    h = in.nextDouble();//读入高
                    shape = new Triangle(a,b,c);//接口回调
                    System.out.print("三角形柱体的体积为:");
                    break;
                case 'y':
                    System.out.print("请分别输入圆形的半径、柱体的高:");
                    a = in.nextDouble();//读入圆的半径
                    h = in.nextDouble();//读入高
                    shape = new Circle(a);//接口回调
                    System.out.print("圆形柱体的体积为:");
                    break;
                case 't':
                    System.out.print("请分别输入梯形的上边和下边、柱体的高:");
                    a = in.nextDouble();//读入梯形的上边
                    b = in.nextDouble();//读入梯形的下边
                    h = in.nextDouble();//读入高
                    shape = new Trapezium(a,b);//接口回调
                    System.out.print("梯形柱体的体积为:");
                    break;
            }
            return shape;//返回接口变量
        }
    }

    Shape.java

    /**
     * 1.创建接口Shape
     * 2.声明抽象方法getArea
     * 3.实现该接口的类都可以使用该方法
     * */
    package cn.edu.ccut.po;
    
    public interface Shape {//接口Shape
        
       public abstract double getArea();//getArea方法声明
       
    }

    Zhuti.java  柱体

    /**
     * 1.主类Zhuti,成员变量 shape high,有参方法Zhuti(),无参方法getVolume(),换底方法huanDi();
     * 2.shape 是接口变量,接收接口的类的对象引用,high代表高
     * 3.Zhuti通过局部变量给成员变量赋值,获取新值,getVolume通过调用对应的面积方法*高求得体积
     * */
    package cn.edu.ccut.po;
    
    public class Zhuti {
        Shape shape;
        double high;
        
        Zhuti(Shape shape,double high){
            this.shape=shape;
            this.high=high;
        }
        double getVolume() {//求体积方法
            return shape.getArea()*high;//柱体体积
        }
        void huanDi(Shape shape) {//换底方法
            this.shape=shape;
        }
    }

    Rectangle.java  矩形

    /**
     * 1.主类矩形类,实现接口Shape,成员变量width,lengh,有参方法Rectangle,无参方法getArea()
     * 2.width,lengh分别代表宽和长,getArea() 求矩形面积
     * 3.Rectangle方法通过局部变量给成员变量赋值,获取新值,getArea()通过成员变量相乘求得面积
     * */
    package cn.edu.ccut.po;
    
    public  class Rectangle implements Shape{//实现接口Shape
        double width;
        double lengh;
        
        Rectangle(double width,double lengh){
            this.width=width;
            this.lengh=lengh;
        }
        public double getArea() {//实现接口中的方法
            return width*lengh;//矩形面积
        }
    }

    Zfx.java  正方形

    /**
     * 1.主类Zfx继承Rectangle类,方法Zfx,无参方法getArea()
     * 2.方法Zfx 传参并调用父类方法,getArea()求面积
     * 3.Zfx通过调用父类方法传给其新的参数,重写getArea方法 两边相乘求得面积
     * */
    package cn.edu.ccut.po;
    
    public  class Zfx extends Rectangle{//继承父类矩形
        Zfx(double side){
            super(side,side);//调用父类方法
        }
        public double getArea() {//重写父类方法
            return width*width;//正方形面积
        }
    }

    Triangle.java  三角形

    /**
     * 1.主类Triangle实现Shape接口,成员变量abc,方法Triangle,面积方法getArea()
     * 2.成员变量abc代表三角形三边,getArea()重写接口的方法
     * 3.getArea()方法重写了接口的方法,根据三边长通过面积公式求得三角形面积
     * */
    package cn.edu.ccut.po;
    
    public class Triangle implements Shape{//实现接口Shape
        double a,b,c;
        Triangle(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));//三角形面积
        }
    
    }

    Circle.java  圆形

    /**
     * 1.主类Circle实现接口Shape,成员变量PI,r,方法getArea()
     * 2.PI代表圆周率,r是圆的半径,getArea()重写接口的方法
     * 3.getArea()重写了接口的方法,通过接受的新半径r 通过公式求得圆的面积
     * */
    package cn.edu.ccut.po;
    
    public class Circle implements Shape{//实现接口Shape
        double PI =3.14;
        double r;
        Circle(double r){
            this.r=r;
        }
        public double getArea() {//实现接口中的方法
            return PI*(r*r);//圆的面积
        }
    
    }

    Trapezium.java  梯形

    /**
     * 1.主类Trapezium梯形继承了矩形类Rectangle,
     * 2.方法Trapezium传参并调用父类方法,方法getArea()求面积
     * 3.Trapezium通过调用父类方法传给其新的参数,重写getArea方法 通过公式求得梯形面积
     * */
    package cn.edu.ccut.po;
    
    public class Trapezium extends Rectangle{//继承矩形类
        Trapezium(double top,double base){
            super(top,base);//调用父类的方法
    
        }
        public double getArea(){//实现接口中的方法
            return (width+lengh)/2;//梯形面积
        }
    
    }

    2.运行结果:

  • 相关阅读:
    公司初创期使用 PHP,为什么很多公司都会慢慢转型到 JAVA
    Firefox 如何对发送的参数进行调试
    Spring security CSRF 跨域访问限制问题
    IntelliJ IDEA 的 Maven 如何设置自动下载源代码和文档
    Spring 的 WebSecurityConfigurerAdapter 过滤器
    Java NIO Path 接口
    Joda-Time – 可用的时区列表
    JWT 如何解码和获得令牌失效的日期
    MySQL索引相关操作
    详细了解INNODB_TRX、INNODB_LOCKs、INNODB_LOCK_waits、PROCESSLIST表
  • 原文地址:https://www.cnblogs.com/zyg777/p/11614806.html
Copyright © 2011-2022 走看看