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.用传递过来的对象调用compute

    package com.homework5;
    
    public interface Compute {
        
        //声明抽象方法
        int computer(int n,int m);
    
    }
    package com.homework5;
    
    public class jia implements Compute {
    
        @Override
        public int computer(int n, int m) {
            
            return n+m;
        }
    
    }
    package com.homework5;
    
    public class jian implements Compute {
    
        @Override
        public int computer(int n, int m) {
            
            return n-m;
        }
    
    }
    package com.homework5;
    
    public class cheng implements Compute {
    
        @Override
        public int computer(int n, int m) {
            
            return n*m;
        }
    
    }
    package com.homework5;
    
    public class chu implements Compute {
    
        @Override
        public int computer(int n, int m) {
            
            return n/m;
        }
    
    }
    package com.homework5;
    
    public class UseCompute {
        
        public void useCom(Compute com, int one, int two)
        {
            System.out.println(com.computer(one, two));
        }
    
    }
    package com.homework5;
    
    public class E {
    
        public static void main(String[] args) {
            
            
            UseCompute uc= new  UseCompute();
            uc.useCom(new jia(), 5, 8);
            uc.useCom(new jian(), 2, 8);
            uc.useCom(new cheng(), 5, 8);
            uc.useCom(new chu(), 40, 8);
    
        }
    
    }

  • 相关阅读:
    C#语言和SQL Server数据库技术_My Bank银行系统
    C#语言和SQL Server数据库技术_深入C#的String类
    C#语言和SQL Server数据库技术_C#语法快速热身
    HTML_利用CSS3制作网页动画
    HTML_定位网页元素
    HTML_浮动
    HTML_盒子模型
    HTML_css3美化网页元素
    iview中select搜索
    第六章、Vue项目预热
  • 原文地址:https://www.cnblogs.com/HRZJ/p/5902401.html
Copyright © 2011-2022 走看看