Java 调用两个不同类的相同方法

Java 调用两个不同类的相同方法,java,oop,Java,Oop,我正在使用一些我无法修改的类: 一般要求 SpecificRequest1(扩展GenericRequest) SpecificRequest2(扩展GenericRequest) 这两个SpecificRequest类共享很多方法(但这些方法没有在GenericRequest中声明,我知道这很糟糕,但我无法更改) 我想创建一个这样的方法(尽可能多地分解): 我知道这不是有效的Java,但这就是想法。你认为有可能用这个做点干净的事吗?或者我必须创建两种不同的方法来处理SpecificRequ

我正在使用一些我无法修改的类:

  • 一般要求
  • SpecificRequest1(扩展GenericRequest)
  • SpecificRequest2(扩展GenericRequest)
这两个SpecificRequest类共享很多方法(但这些方法没有在GenericRequest中声明,我知道这很糟糕,但我无法更改)

我想创建一个这样的方法(尽可能多地分解):


我知道这不是有效的Java,但这就是想法。你认为有可能用这个做点干净的事吗?或者我必须创建两种不同的方法来处理
SpecificRequest1
SpecificRequest2

这是一种众所周知的模式适配器。代码可能如下所示:

class GenericRequest {}

class SpecificRequest1 extends GenericRequest {
    void method1() {
        System.out.println("specific1");
    }
}

class SpecificRequest2 extends GenericRequest {
    void method2() {
        System.out.println("specific2");
    }
}

interface ReqInterface {
    void method();
}

class Specific1 implements ReqInterface {
    private final SpecificRequest1 req =new SpecificRequest1();

    @Override
    public void method() {
        req.method1();
    }
}

class Specific2 implements ReqInterface {
    private final SpecificRequest2 req =new SpecificRequest2();

    @Override
    public void method() {
        req.method2();
    }
}

public class Production {
    void method(ReqInterface req) {
        req.method();
    }

    public static void main(String[] args) {
        Production l3 = new Production();
        l3.method(new Specific1());
        l3.method(new Specific2());
    }
}

尽量避免方法参数和
instanceof
中的布尔值(不惜任何代价))

这是一个众所周知的模式适配器。代码可能如下所示:

class GenericRequest {}

class SpecificRequest1 extends GenericRequest {
    void method1() {
        System.out.println("specific1");
    }
}

class SpecificRequest2 extends GenericRequest {
    void method2() {
        System.out.println("specific2");
    }
}

interface ReqInterface {
    void method();
}

class Specific1 implements ReqInterface {
    private final SpecificRequest1 req =new SpecificRequest1();

    @Override
    public void method() {
        req.method1();
    }
}

class Specific2 implements ReqInterface {
    private final SpecificRequest2 req =new SpecificRequest2();

    @Override
    public void method() {
        req.method2();
    }
}

public class Production {
    void method(ReqInterface req) {
        req.method();
    }

    public static void main(String[] args) {
        Production l3 = new Production();
        l3.method(new Specific1());
        l3.method(new Specific2());
    }
}

尽量避免方法参数和
instanceof
中的布尔值(不惜任何代价))

根据您所述,SpecificRequests 1和SpecificRequests 2似乎实现了具有相同名称的方法,但该方法名称在GenericRequest中不存在,对吗?我不确定是否理解。除了作用域之外,它是完全有效的Java,但毫无意义,因为您可以只
instanceof
并强制转换。@AndrewFan是的,正确。您可能应该为每个子类定义一个公共接口和一个实现该接口的适配器类,只需将其委托给子类实例。。。。对于
generirequest
,情况可能也一样,
generirequestinterface
SpecificRequestInterface
的超类型。这将帮助您克服
GenericRequest
本身不是
SpecificRequestInterface
超类型的问题。从您所说的,SpecificRequest 1和SpecificRequest 2似乎实现了同名的方法,但该方法名称在GenericRequest中不存在,对吗?我不确定我是否理解。除了作用域之外,它是完全有效的Java,但毫无意义,因为您可以只
instanceof
并强制转换。@AndrewFan是的,正确。您可能应该为每个子类定义一个公共接口和一个实现该接口的适配器类,只需将其委托给子类实例。。。。对于
generirequest
,情况可能也一样,
generirequestinterface
SpecificRequestInterface
的超类型。这将帮助您克服
generirequest
本身不是
SpecificRequestInterface
的超类型的问题。感谢您提供的详细答案!谢谢你的详细回答!