class Test{ public static void main(String[] args) { fun i=new fun(); jiafa s1=new jiafa(); jianfa s2=new jianfa(); chengfa s3=new chengfa(); chufa s4=new chufa(); System.out.print("加法结果:");i.func(s1,6,3); System.out.print("减法结果:"); i.func(s2,6,3); System.out.print("乘法结果:"); i.func(s3,6,3); System.out.print("除法结果:"); i.func(s4,6,3); } } interface Ijisuanqi{ int jisuan(int a,int b); } class fun{ public void func(Ijisuanqi per,int a,int b){ System.out.println(per.jisuan(a,b)); } } class jiafa implements Ijisuanqi{ public int jisuan(int a,int b){ return a+b; } } class jianfa implements Ijisuanqi{ public int jisuan(int a,int b){ return a-b; } } class chengfa implements Ijisuanqi{ public int jisuan(int a,int b){ return a*b; } } class chufa implements Ijisuanqi{ public int jisuan(int a, int b) { if (b == 0) { return -1; } else { return a / b; } } }
运行结果: