简体   繁体   English

返回实现接口的对象

[英]Returning objects implementing interfaces

I have a given set of interfaces, and I have to implement them. 我有一组给定的接口,我必须实现它们。 The interfaces are the following: 接口如下:

package com.something.interfaces;
public interface A {
    void foo();
    Set<B> bar();
}

package com.something.interfaces;
public interface B {
    void foobar();
}

And my implementations are the following: 我的实现如下:

package com.something.implementations;
public class A implements com.something.interfaces.A {
    @Override 
    public void foo(){
        //Something
    }

    @Override
    public Set<com.something.interfaces.B> bar(){
        Set<com.something.implementations.B> something = new HashSet<com.something.implementations.B>();
        //...
        return something;
    }
}

package com.something.implementations;
public class B implements com.something.interfaces.B {
    @override
    public void foobar(){
        //Something
    }
}

The problem is in the method bar() of the implemented class. 问题出在已实现类的方法bar()中。 It gives me the error: 它给了我错误:

Type mismatch: cannot convert from java.util.Set<com.something.interfaces.B> to java.util.Set<com.something.implementations.B>

I am 100% sure that the signature are respected, I tried just leaving the auto generated stubs to check. 我100%确保签名得到尊重,我尝试只留下自动生成的存根进行检查。 Maybe there is something I am missing on the theory, but how can I implement the method if this does not work? 也许我在理论上缺少一些东西,但是如果这种方法不起作用怎么办?

Thanks 谢谢

EDIT As this question has been marked as a possible duplicate, I will explain the difference with it. 编辑由于此问题已被标记为可能重复,因此我将解释与它的区别。

That was a generic question, I have a really specific problem here. 那是一个普遍的问题,我在这里有一个非常具体的问题。 The interfaces are not accessible by me, because I am writing code that will be submitted and autograded using original interfaces, I won't submit any modifications. 我无法访问这些接口,因为我正在编写将使用原始接口提交并自动分级的代码,因此我不会提交任何修改。

Knowing what that question pointed out does not help me understand how to escape from this situation, because given these interfaces, I need to return my implemented versions of them, and that question really gave me no clue on how to do that. 知道该问题指出的内容并不能帮助我理解如何摆脱这种情况,因为在给定这些接口的情况下,我需要返回它们的已实现版本,而该问题确实使我不知道如何执行此操作。

EDIT2 编辑2

In the comments, it has been pointed out that my requirements are not clear. 在评论中,有人指出我的要求不清楚。 I'll try to clarify. 我会尽力澄清。

I can't change the interfaces, I need to be sure that this is a problem of interfaces and that there is not a workaround for this. 我无法更改接口,我需要确保这是接口问题,并且没有解决方法。 If this is the case, I will try to reason with my professor. 如果是这样,我将尝试与我的教授进行推理。

Change your implementation of the bar() method to this: 将您的bar()方法的实现更改为此:

@Override
public Set<com.something.interfaces.B> bar(){
    Set<com.something.interfaces.B> something = new HashSet<com.something.interfaces.B>();
    //...
    return something;
}

You can fill the Set in something with any object which implements the com.something.interfaces.B interface, including your implementation class com.something.implementations.B . 您可以填写设置的something与它实现了任何物体com.something.interfaces.B接口,包括您的实现类com.something.implementations.B

Change your interface A like this: 像这样更改您的界面A:

public interface A {
    void foo();
    Set<com.something.interfaces.B> bar();
}

Change the names of your interfaces or your implementations to avoid confusion 更改接口或实现的名称以避免混淆

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

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