继承和多个协作者 [英] Inheritance and Multiple Collaborators

查看:127
本文介绍了继承和多个协作者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个Java接口/ ABCs /类:

pre $ public abstract class Target {
public abstract void fire (负载);
}

public class HttpTarget extends Target {
@Override
public void fire(Load load){
// ...
}
}

public interface Load {
// ...
}

public class HttpLoad implements Load {
// ...
}

//驱动程序内部
Target target = testSuite.getTarget();
Load load = testSuite.getLoad();

target.fire(load);

因此基本上是 Target can fire() a 加载。我的主应用程序 Driver 不关心 getTarget返回的是什么样的 Target ,或者 getLoad()返回什么样的 Load



我想改变 fire() HttpTarget 中的方法定义为:

  @Override 
public void fire(HttpLoad httpLoad){
// ...
}

但是当我这样做时,Java会抱怨方法覆盖与它的父元素 Target class提供的定义不匹配(如 Load HttpLoad 是两个不同的东西)。

这里有什么解决方案?泛型?抽象工厂? 最终,我希望能够强制执行 HttpTarget fire()方法只能接受 HttpLoad s,但仍然与 Driver 代码兼容。有人可以提供代码示例吗?提前致谢!

解决方案

是的,您需要泛型:

  public abstract class Target< L extends Load> {
public abstract void fire(L load);
}

公共类HttpTarget扩展目标< HttpLoad> {
@Override
public void fire(HttpLoad load){
...
}
}

public interface TestSuite< L extends负载> {//或者类
L getLoad();
目标< L> getTarget方法();
}

公共类HttpTestSuite实现TestSuite< HttpLoad> {
@Override
public HttpLoad getLoad(){
...
}

@Override
public Target< HttpLoad> getTarget(){
return new HttpTarget();




$ b

Java拒绝编译你的HttpTarget类的原因是因为它不会覆盖Target的 fire(Load)方法。事实上,根据合同,目标应该接受任何类型的负载作为参数。并且 HttpTarget fire()方法只接受HttpLoad的实例,因此打破了Liskov原理。泛型是解决这个问题的方法。


I have several Java interfaces/ABCs/classes:

public abstract class Target {
    public abstract void fire(Load load);
}

public class HttpTarget extends Target {
    @Override
    public void fire(Load load) {
        // ...
    }
}

public interface Load {
    // ...
}

public class HttpLoad implements Load {
    // ...
}

// Inside a driver
Target target = testSuite.getTarget();
Load load = testSuite.getLoad();

target.fire(load);

So essentially a Target can fire() a Load. My main app Driver doesn't care about what kind of Target is returned by getTarget(), or what kind of Load is returned by getLoad(). It's job is to make sure that a load is fired.

I'd like to change the fire() method definition inside HttpTarget to:

@Override
public void fire(HttpLoad httpLoad) {
    // ...
}

However when I do that, Java complains that the method override doesn't match the definition provided by its parent Target class (as Load and HttpLoad are two different things).

What's the solution here? Generics? Abstract factories? Ultimately, I want to be able to enforce that HttpTarget's fire() method can only accept HttpLoads, but still be compatible with the Driver code. Can someone provide a code example? Thanks in advance!

解决方案

Yes, you would need generics:

public abstract class Target<L extends Load> {
    public abstract void fire(L load);
}

public class HttpTarget extends Target<HttpLoad> {
    @Override
    public void fire(HttpLoad load) {
        ...
    }
}

public interface TestSuite<L extends Load> { // or class
    L getLoad();
    Target<L> getTarget();
}

public class HttpTestSuite implements TestSuite<HttpLoad> {
    @Override
    public HttpLoad getLoad() {
        ...
    }

    @Override
    public Target<HttpLoad> getTarget() {
        return new HttpTarget();
    }
}

The reason Java refuses to compile your HttpTarget class is because it doesn't override the Target's fire(Load) method. Indeed, a Target, by contract is supposed to accept any kind of Load as argument. And the HttpTarget's fire() method only accepts instances of HttpLoad, and thus breaks the Liskov principle. Generics are the solution to this problem.

这篇关于继承和多个协作者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆