简体   繁体   English

用不同的实现调用相同的方法签名

[英]Call same method signature with different implementations

Given: I have two different projects legacy A and nextGen B. There is the third project C which is shared between A and B. 给定的:我有两个不同的项目,即旧版A和nextGenB。还有第三个项目C,这在A和B之间共享。

Objective: I have to do an averageCalculation() in the third project C. But the implementation is different for the projects A and B. Using the same method signature but different implementations, how do I create a design? 目标:我必须在第三个项目C中执行一个averageCalculation()。但是对于项目A和B,实现方式是不同的。使用相同的方法签名但实现方式不同,如何创建设计? Note: the project A and B should just call averageCalulation() the same method signature. 注意:项目A和B应该只调用averageCalulation()相同的方法签名。

Project C 项目C

Interface I {
averageCalculation();
}

Class CClass implements I{
?<averageCalculation()-for A>
?<averageCalculation()- for B>
}

Project A 项目A

{
I i1 = new CClass();
i1.averageCalculation();
}

Project B 项目B

{
I i2 = new CClass();
i2.averageCalculation();
}

Is the above approach correct? 以上方法正确吗? if so how would i create two implementations of averageCalculation() in CClass? 如果是这样,我将如何在CClass中创建averageCalculation()的两个实现?

Create two different classes that implement your interface, and use a different class in each project: 创建两个实现您的界面的不同类,并在每个项目中使用一个不同的类:

Project C 项目C

interface I {
averageCalculation();
}

class CClassForA implements I{
    averageCalculation(){...} // for A
}

class CClassForB implements I{
    averageCalculation(){...} // for B
}

Project A 项目A

{
I i1 = new CClassForA();
i1.averageCalculation();
}

Project B 项目B

{
I i2 = new CClassForB();
i2.averageCalculation();
}

An interface is just a contract for classes that implement it to define. 接口只是实现它定义的类的协定。 If a class needs two different implementations of a single method in an interface then you should consider redesigning your project 如果类需要接口中单个方法的两种不同实现,则应考虑重新设计项目

Why do you need CClass ? 为什么需要CClass? You can have a class in Project A implement I and another class in Project B doing the same. 您可以在Project A实施I中有一个类,而在Project B中有另一个类也可以这样做。

EDIT : the compiler will not let you have two different implementations of the method with the same signature. 编辑:编译器将不允许您使用具有相同签名的方法的两个不同实现。 You do have an option to overload it if that is what you want 您确实可以选择是否要重载它

public interface SomeInter 
{
   public void doSomething();
}


public class ImplClass implements SomeInter
{

    @Override
    public void doSomething() {
        // TODO Auto-generated method stub

    }


    public void doSomething(String abc) 
    {
        // TODO Auto-generated method stub

    }

}

Hope this helps!! 希望这可以帮助!!

I am not sure I understand your problem entirely but here is a solution. 我不确定我是否完全理解您的问题,但这是解决方案。 You can't change the legacy projects but you want to A and B to conform to some Interface I. You can do this by wrapping A and B in something that does conform to I and implement I with A and B's respective implementations. 您不能更改旧项目,但是希望A和B符合某些接口I。您可以通过将A和B包装在符合I的内容中,并使用A和B的各自实现来实现I。

public class Problem {

    public static class A{
        public int foo(){
            return 3;
        }
    }

    public static class B{
        public int foo(){
            return 5;
        }
    }

    public interface TheFoo{
        public int foo();
    }

    public static class AWrapper extends A implements TheFoo{
        public int foo(){
            return super.foo();
        }
    }

    public static class BWrapper extends B implements TheFoo{
        public int foo(){
            return super.foo();
        }
    }

    public static void main(String[] args){

        //TheFoo[] myFoos = new TheFoo[]{new A(), new B()}; Won't work
        TheFoo[] myFoos = new TheFoo[]{new AWrapper(), new BWrapper()};
        for(TheFoo curFoo : myFoos){
            System.out.println(curFoo.foo());
        }
    }   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM