Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 泛型类型扩展接口,但获取不兼容的类型错误_Java_Generics_Inheritance_Interface_Polymorphism - Fatal编程技术网

Java 泛型类型扩展接口,但获取不兼容的类型错误

Java 泛型类型扩展接口,但获取不兼容的类型错误,java,generics,inheritance,interface,polymorphism,Java,Generics,Inheritance,Interface,Polymorphism,我是Java新手——来自PHP和Python世界。我确信这是一个简单的问题,我遗漏了一些基本的东西,但经过几个小时的搜索和修补,我无法找到它 我有一个保持指向点的Path类,一个In和一个Out: public class Path<In extends PointInterface, Out extends PointInterface> { private In in; private Out out; public Path(In in, Out ou

我是Java新手——来自PHP和Python世界。我确信这是一个简单的问题,我遗漏了一些基本的东西,但经过几个小时的搜索和修补,我无法找到它

我有一个保持指向点的
Path
类,一个
In
和一个
Out

public class Path<In extends PointInterface, Out extends PointInterface> {
    private In in;
    private Out out;

    public Path(In in, Out out) {
        this.in = in;
        this.out = out;
    }

    public In getIn() {
        return in;
    }

    public Out getOut() {
        return out;
    }
}
路径
如上所述

点接口

Path path = new Path<>(new Endpoint(), new Queue());

InHandlerInterface handler = path.getIn().getInHandler(); // returns the EndpointInHandler
handler.handle(path);
public interface PointInterface {
    InHandlerInterface getInHandler();
}
public class Endpoint implements PointInterface {
    @Override
    public InHandlerInterface getInHandler() {
        return new EndpointInHandler();
    }

    public String specificToEndpoint() {
        return "Whatever";
    }
}
public interface InHandlerInterface {
    void handle(Path path);
}
端点

Path path = new Path<>(new Endpoint(), new Queue());

InHandlerInterface handler = path.getIn().getInHandler(); // returns the EndpointInHandler
handler.handle(path);
public interface PointInterface {
    InHandlerInterface getInHandler();
}
public class Endpoint implements PointInterface {
    @Override
    public InHandlerInterface getInHandler() {
        return new EndpointInHandler();
    }

    public String specificToEndpoint() {
        return "Whatever";
    }
}
public interface InHandlerInterface {
    void handle(Path path);
}
在HandlerInterface中

Path path = new Path<>(new Endpoint(), new Queue());

InHandlerInterface handler = path.getIn().getInHandler(); // returns the EndpointInHandler
handler.handle(path);
public interface PointInterface {
    InHandlerInterface getInHandler();
}
public class Endpoint implements PointInterface {
    @Override
    public InHandlerInterface getInHandler() {
        return new EndpointInHandler();
    }

    public String specificToEndpoint() {
        return "Whatever";
    }
}
public interface InHandlerInterface {
    void handle(Path path);
}
endpointHandler

public class EndpointInHandler implements InHandlerInterface {
    @Override
    public void handle(Path path) {
        Endpoint in = path.getIn(); // This is giving me the Incompatible types error

        String whatever = in.specificToEndpoint();
    }
}

当您执行
Path Path=new Path(new Endpoint(),new Queue())
时,实际上已经丢失了泛型的类型

您需要将其编写为
Path Path=new Path(new Endpoint(),new Queue())
,以便编译器知道泛型引用的实际类型

更新 再看一遍,我意识到你需要把泛型放在任何地方。看到泛型到处出现可能看起来很奇怪,但这是确保编译时类型安全的方法。这样做,您就不需要任何显式强制转换

Path<Endpoint, Queue> path = new Path<>(new Endpoint(), new Queue());

InHandlerInterface<Endpoint> handler = path.getIn().getInHandler(); // returns the EndpointInHandler
handler.handle(path);
路径路径=新路径(新端点(),新队列()); InHandlerInterface handler=path.getIn().getInHandler();//返回EndpointHandler handle.handle(路径); 点接口:

public interface PointInterface<T extends PointInterface> {
    InHandlerInterface<T> getInHandler();
}
public interface InHandlerInterface<T extends PointInterface<T>> {
    void handle(Path<T, ?> path);
}
公共接口点接口{
InHandler接口getInHandler();
}
终点:

public class Endpoint implements PointInterface<Endpoint> {
    @Override
    public InHandlerInterface<Endpoint> getInHandler() {
        return new EndpointInHandler<>();
    }

    public String specificToEndpoint() {
        return "Whatever";
    }
}
public类端点实现PointInterface{
@凌驾
公共InHandler接口getInHandler(){
返回新的EndpointHandler();
}
公共字符串specificToEndpoint(){
返回“无论如何”;
}
}
在HandlerInterface中:

public interface PointInterface<T extends PointInterface> {
    InHandlerInterface<T> getInHandler();
}
public interface InHandlerInterface<T extends PointInterface<T>> {
    void handle(Path<T, ?> path);
}
HandlerInterface中的公共接口{ 无效句柄(路径); } EndpointHandler:

public class EndpointInHandler implements InHandlerInterface<Endpoint> {
    @Override
    public void handle(Path<Endpoint, ?> path) {
        Endpoint in = path.getIn(); // This is naturally type safe, and compiler won't complain

        String whatever = in.specificToEndpoint();
    }
}
公共类EndpointHandler在Handler接口中实现{
@凌驾
公共无效句柄(路径){
Endpoint in=path.getIn();//这自然是类型安全的,编译器不会抱怨
字符串whatever=in.specifictToEndpoint();
}
}

当您执行
Path Path=new Path(new Endpoint(),new Queue())
时,您实际上已经丢失了泛型的类型

您需要将其编写为
Path Path=new Path(new Endpoint(),new Queue())
,以便编译器知道泛型引用的实际类型

更新 再看一遍,我意识到你需要把泛型放在任何地方。看到泛型到处出现可能看起来很奇怪,但这是确保编译时类型安全的方法。这样做,您就不需要任何显式强制转换

Path<Endpoint, Queue> path = new Path<>(new Endpoint(), new Queue());

InHandlerInterface<Endpoint> handler = path.getIn().getInHandler(); // returns the EndpointInHandler
handler.handle(path);
路径路径=新路径(新端点(),新队列()); InHandlerInterface handler=path.getIn().getInHandler();//返回EndpointHandler handle.handle(路径); 点接口:

public interface PointInterface<T extends PointInterface> {
    InHandlerInterface<T> getInHandler();
}
public interface InHandlerInterface<T extends PointInterface<T>> {
    void handle(Path<T, ?> path);
}
公共接口点接口{
InHandler接口getInHandler();
}
终点:

public class Endpoint implements PointInterface<Endpoint> {
    @Override
    public InHandlerInterface<Endpoint> getInHandler() {
        return new EndpointInHandler<>();
    }

    public String specificToEndpoint() {
        return "Whatever";
    }
}
public类端点实现PointInterface{
@凌驾
公共InHandler接口getInHandler(){
返回新的EndpointHandler();
}
公共字符串specificToEndpoint(){
返回“无论如何”;
}
}
在HandlerInterface中:

public interface PointInterface<T extends PointInterface> {
    InHandlerInterface<T> getInHandler();
}
public interface InHandlerInterface<T extends PointInterface<T>> {
    void handle(Path<T, ?> path);
}
HandlerInterface中的公共接口{ 无效句柄(路径); } EndpointHandler:

public class EndpointInHandler implements InHandlerInterface<Endpoint> {
    @Override
    public void handle(Path<Endpoint, ?> path) {
        Endpoint in = path.getIn(); // This is naturally type safe, and compiler won't complain

        String whatever = in.specificToEndpoint();
    }
}
公共类EndpointHandler在Handler接口中实现{
@凌驾
公共无效句柄(路径){
Endpoint in=path.getIn();//这自然是类型安全的,编译器不会抱怨
字符串whatever=in.specifictToEndpoint();
}
}

你的问题似乎有点让人困惑。你能给出一个可复制的示例代码,并指出错误在代码中的位置吗?“这样你会更快地得到解决方案。”我可以试试Codebender,但它有几个类。让我试着配对到最小值。@Codebender有帮助吗?即使您将所有泛型传播到它们需要去的地方,在调用
path.getIn()
时,您只知道
getIn()
返回一些实现
PointInterface
的未知类。不能保证它是一个
端点
。泛型非常有用,但它们并不像来自另一种语言的人所看到的那样强大。它们实际上是编译时唯一的语法糖,用于强制执行类型安全并消除一些强制转换。泛型类型信息不会传播到运行时,因此您试图用泛型完成的工作无法完成。您的问题似乎有点困惑。你能给出一个可复制的示例代码,并指出错误在代码中的位置吗?“这样你会更快地得到解决方案。”我可以试试Codebender,但它有几个类。让我试着配对到最小值。@Codebender有帮助吗?即使您将所有泛型传播到它们需要去的地方,在调用
path.getIn()
时,您只知道
getIn()
返回一些实现
PointInterface
的未知类。不能保证它是一个
端点
。泛型非常有用,但它们并不像来自另一种语言的人所看到的那样强大。它们实际上是编译时唯一的语法糖,用于强制执行类型安全并消除一些强制转换。泛型类型信息不会传播到运行时,因此您试图用泛型完成的任务无法完成。谢谢!这两种解决方案都奏效了。对更新的一个更正是:我不得不将
添加到
公共类EndpointInHandler
,即使从未使用过
T
。否则,它会抛出一个错误,即我在非泛型类型上使用了菱形运算符。您可能希望在接口中将定义写成
(已添加末尾的
)@Lino这一点很好。这就是不使用IDE回答的问题。谢谢!这两个都是