码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-09-24 09:19:56      阅读:580      评论:0      收藏:0      [点我收藏+]

标签:

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);

    }

}

技术分享

 

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

标签:

原文地址:http://www.cnblogs.com/HRZJ/p/5902401.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!