zoukankan      html  css  js  c++  java
  • 接口-计算

    利用接口做参数,写个计算器,能完成+-*/运算

    (1)定义一个接口Compute含有一个方法int computer(int n,int m);

    (2)设计四个类分别实现此接口,完成+-*/运算

    (3)设计一个类UseCompute,含有方法:

    public void useCom(Compute com, int one, int two)

    此方法要求能够:1.用传递过来的对象调用computer方法完成运算

                    2.输出运算的结果

    (4)设计一个测试类,调用UseCompute中的方法useCom来完成+-*/运算

    package homework;
    
    public interface Computer {
        
        int computer(int n,int m);
    
    }
    package homework;
    
    public class ClassJia implements Computer {
    
        @Override
        public int computer(int n, int m) {
            // TODO 自动生成的方法存根
            //System.out.println("m+n="+(m+n));
            return m+n;
        }
    
    }
    package homework;
    
    public class ClassJian implements Computer {
    
        @Override
        public int computer(int n, int m) {
            // TODO 自动生成的方法存根
            //System.out.println("m-n="+(m-n));
            return m-n;
        }
    
    }
    package homework;
    
    public class ClassCheng implements Computer {
    
        @Override
        public int computer(int n, int m) {
            // TODO 自动生成的方法存根
            //System.out.println("m*n="+m*n);
            return m*n;
        }
    
    }
    package homework;
    
    public class ClassChu implements Computer {
    
        @Override
        public int computer(int n, int m) {
            // TODO 自动生成的方法存根
            //System.out.println("m/n="+m/n);
            return m/n;
        }
    
    }
    package homework;
    
    public class UseComputer {
        
        
        
        public void useCom(Computer com, int one, int two)
        {
            //com.computer(one, two);
            System.out.println("运行结果是:"+com.computer(one, two));
            
            
            
        }
        
        
        
        
        
        
        
        
    
    }
    package homework;
    
    public class TestComputer {
    
        public static void main(String[] args) {
            // TODO 自动生成的方法存根
    
            UseComputer uc=new UseComputer();
            ClassJia cjia=new ClassJia();
            uc.useCom(cjia, 3, 4);
        
            ClassJian cjian=new ClassJian();
            uc.useCom(cjian, 12, 34);
            
            ClassCheng cc=new ClassCheng();
            uc.useCom(cc, 9, 4);
            
            ClassChu cchu=new ClassChu();
            uc.useCom(cchu, 8, 16);
            
            
        
            
            
        }
    
    }

    运行结果:

  • 相关阅读:
    齐次和线性
    数组指针/指针数组
    坐标转换矩阵
    【转】GMM与K-means聚类效果实战
    利用虚函数实现多态的方式:动态绑定
    类型限定符volatile
    《剑指offer》查找二维数组内元素 c++
    windows下使用命令行编译、链接C++源文件
    关于该博客的美化
    vimium快捷键修改
  • 原文地址:https://www.cnblogs.com/miss123/p/5533403.html
Copyright © 2011-2022 走看看