interface IComputer{
int computer(int n, int m);
}
//Jiafa类实现接口IComputer
class Jiafa implements IComputer{
public int computer(int n, int m){
return n+m;
}
}
//Jianfa类实现接口IComputer
class Jianfa implements IComputer{
public int computer(int n, int m){
return n-m;
}
}
//Chengfa类实现接口IComputer
class Chengfa implements IComputer {
@Override
public int computer(int n, int m) {
return n * m;
}
}
//Chufa类实现接口IComputer
class Chufa implements IComputer {
@Override
public int computer(int n, int m) {
return n/m;
}
}
//在类中定义一个方法,此方法能够用传递过来的对象调用computer方法完成运算,并输出运算的结果
class UseComputer{
public void useCom(IComputer com,int one,int two){
System.out.println(com.computer(one,two));
}
}
public class Test{
public static void main(String[] args) {
UseComputer useComputer = new UseComputer() ;
useComputer.useCom(new Jiafa(),1,1);
UseComputer useComputer1=new UseComputer();
useComputer1.useCom(new Jianfa(),22,19);
UseComputer useComputer2=new UseComputer();
useComputer2.useCom(new Chengfa(),11,7);
UseComputer useComputer3=new UseComputer();
useComputer3.useCom(new Chufa(),10,2);
}
}
用java实现小程序——使用接口实现简易计算器
最新推荐文章于 2024-06-23 14:56:37 发布