面向对象-同一个需求-五种开发方式-答案(第1种)

package com.work.jisuan;

public interface IAdd {
    int add(int a,int ...b);
}
package com.work.jisuan;

public interface ISub {
    int sub(int a,int...b);
}
package com.work.jisuan;

public class MyAdd implements IAdd{
    @Override
    public int add(int a, int... b) {
        int sum=a;
        for(int i=0;i<b.length;i++){
            sum+=b[i];
        }
        return sum;
    }
}
package com.work.jisuan;

public class MySub implements ISub{
    @Override
    public int sub(int a, int... b) {
        int sum=a;
        for(int i=0;i<b.length;i++){
            sum-=b[i];
        }
        return sum;
    }
}

 

package com.work.jisuan;

public class MyMath1 {
    public static int add(int a,int ...b){
        int sum=a;
        for(int i=0;i<b.length;i++){
            sum+=b[i];
        }
        return sum;
    }
    public static int sub(int a,int ...b){
        int sum=a;
        for(int i=0;i<b.length;i++){
            sum-=b[i];
        }
        return sum;
    }
}

 

package com.work.jisuan;

public class MyMath2 {
    private int num1;
    private int []num2;
    public MyMath2(int a,int []b){
        this.num1=a;
        this.num2=b;
    }
    public int add(){
        int sum=this.num1;
        for(int i=0;i<this.num2.length;i++){
            sum+=num2[i];
        }
        return sum;
    }
    public  int sub(){
        int sum=this.num1;
        for(int i=0;i<this.num2.length;i++){
            sum-=num2[i];
        }
        return sum;
    }
}

 

 

package com.work.jisuan;

public class MyMath3 implements ISub,IAdd{
    @Override
    public int add(int a, int... b) {
        int sum=a;
        for(int i=0;i<b.length;i++){
            sum+=b[i];
        }
        return sum;
    }

    @Override
    public int sub(int a, int... b) {
        int sum=a;
        for(int i=0;i<b.length;i++){
            sum-=b[i];
        }
        return sum;
    }
}

 

package com.work.jisuan;

public class MyMath4 {
        public IAdd addMember; //可以优化为private
        public ISub subMember;
        public MyMath4(IAdd addMember,ISub subMember){
            this.addMember=addMember;
            this.subMember=subMember;
        }
}

 

package com.work.jisuan;

public class MyMath5 implements ISub,IAdd{
    private IAdd addMember;
    private ISub subMember;
    public MyMath5(IAdd addMember,ISub subMember){
        this.addMember=addMember;
        this.subMember=subMember;
    }

    @Override
    public int add(int a, int... b) {
       return this.addMember.add(a,b);
    }

    @Override
    public int sub(int a, int... b) {
     return this.subMember.sub(a,b);
    }
}

 

测试类:

package com.work.jisuan;

public class Test1 {
    public static void main(String[] args) {
        int a=1;
        int []b={2,3,4,5};
        MyAdd myAdd=new MyAdd();
        MySub mySub=new MySub();
        //版本一
        System.out.println( MyMath1.add(a,b));
        System.out.println( MyMath1.sub(a,b));

        //版本二
        MyMath2 myMath2=new MyMath2(a,b);
        System.out.println(myMath2.add());
        System.out.println(myMath2.sub());

        //版本三
        MyMath3 myMath3 = new MyMath3();
        System.out.println( myMath3.add(a,b));
        System.out.println( myMath3.sub(a,b));

        //版本四
        MyMath4 myMath4=new MyMath4(myAdd,mySub);
        System.out.println(myMath4.addMember.add(a,b));
        System.out.println(myMath4.subMember.sub(a,b));

        //版本五
        MyMath5 myMath5=new MyMath5(myAdd,mySub);
        System.out.println(myMath5.add(a,b));
        System.out.println(myMath5.sub(a,b));

    }
}

 

posted @ 2022-05-24 23:13  xiaoyongdata  阅读(52)  评论(0编辑  收藏  举报