zoukankan      html  css  js  c++  java
  • 38.利用接口做参数,写个计算器,能完成+-*/运算 (1)定义一个接口Compute含有一个方法int computer(int n,int m); (2)设计四个类分别实现此接口,完成+-*/运算 (3)设计一个类UseCompute,含有方法: public void useCom(Compute com, int one, int two) 此方法要求能够:1.用传递过来的对象调用comp

    //接口Compute
    package jieKou;

    public interface Compute {
    int Computer(int n,int m);

    }

    //加
    package jieKou;

    public class Jia implements Compute {

    @Override
    public int Computer(int n, int m) {
    	// TODO 自动生成的方法存根
    	return m+n;
    }
    

    }

    //减
    package jieKou;

    public class Jian implements Compute {

    @Override
    public int Computer(int n, int m) {
    	// TODO 自动生成的方法存根
    	return n-m;
    }
    

    }

    //乘
    package jieKou;

    public class Cheng implements Compute {

    @Override
    public int Computer(int n, int m) {
    	// TODO 自动生成的方法存根
    	return n*m;
    }
    

    }

    //除

    package jieKou;

    public class Chu implements Compute {

    @Override
    public int Computer(int n, int m) {
    	// TODO 自动生成的方法存根
    	return n/m;
    }
    

    }

    //UseCompute

    package jieKou;

    public class UseCompute {
    public void useCom(Compute com, int one, int two)
    {
    System.out.println(com.Computer(one, two));
    }

    }
    //测试类

    package jieKou;

    public class Test001 {

    public static void main(String[] args) {
    	UseCompute a=new UseCompute();
    	a.useCom(new Jia(), 5, 6);
    	a.useCom(new Jian(), 5, 6);
    	a.useCom(new Cheng(), 5, 6);
    	a.useCom(new Chu(), 30, 6);
    }
    

    }

    ///运行结果

    11
    -1
    30
    5

  • 相关阅读:
    ceph服务日志分析
    ceph 守护进程管理
    ceph 数据一致性检查(scrub)
    ceph osd坏盘更换
    OSD操作(扩容/缩容/换盘/数据重平衡/数据一致性)
    SharePoint REST API 获取文件夹下的项目数
    SharePoint REST API 设置SummaryLength属性
    庄子逍遥哲学的六个主要思想
    详细解说,无人机构造及原理
    如何写好项目规划和方案设计文档
  • 原文地址:https://www.cnblogs.com/nicebaby/p/5903433.html
Copyright © 2011-2022 走看看