繁体   English   中英

当 class 扩展时,如何在 java 中定义 class 类型的变量?

[英]how to define a variable of a class type in java when the class has extends?

我有这个 class:

   public class FindPath<T,N extends Path<T,N>> {

    public FindPath() {
        
   // the constructor ...
    }
}

和路径接口:

public interface Path<N, P extends Path<N,P>>
            extends Iterable<N>, Comparable<Path<?,?>>

其中 T、P 和 N 是泛型类型。 我想在另一个 class 中定义一个变量 FindPath,并调用它的构造函数,基本上我想要这样的东西:

public class TestD {

private FindPath <WNode,WNodePath extends Path<WNode,WNodePath>> Find_Path = new FindPath;

} 

WNode,WNodePath是我在泛型类型中使用的另一个类)

我究竟做错了什么? 如何做到这一点?

还有一个问题:|| WNodePath class 的定义如下:

public class WNodePath implements Path<WNode, WNodePath > { .. }

路径接口是Comparable ,这是否意味着WNodePath也是 Comparable ,这意味着我可以在优先级队列中使用它compareTo() function 吗?

当您声明FindPath引用时,您不使用extends ,它用于为该类型参数合法使用的类提供边界(限制)。 (当您有通配符类型参数时,您可以使用扩展,但这与此处无关,请参阅https://docs.oracle.com/javase/tutorial/extra/generics/wildcards.ZFC35EZ8830D5FC69D807BADE5Z

说啊:

    private FindPath<WNode,WNodePath> findPath = new FindPath<>();

哪个编译。

是的, WNodePath实现了Comparable ,通过Path传递。

暂无
暂无

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

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