当 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
是我在通用类型中使用的另一个 classes )
我做错了什么?如何正确执行此操作?
还有一个问题:|| WNodePath
class 的定义如下:
public class WNodePath implements Path<WNode, WNodePath > { .. }
路径接口是 Comparable
,这是否意味着 WNodePath
也是 Comparable ,这意味着我可以在优先级队列中使用它 compareTo()
功能?
当您声明 FindPath
引用时,您不使用 extends
,它用于在 类 上提供边界(限制),用于该类型参数是合法的. (当你有一个通配符类型参数时,你可以使用扩展,但这与这里无关,参见https://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html)
直接说:
private FindPath<WNode,WNodePath> findPath = new FindPath<>();
编译。
是的,WNodePath
实现了 Comparable
,通过 Path
传递。
我有这个 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
是我在通用类型中使用的另一个 classes )
我做错了什么?如何正确执行此操作?
还有一个问题:|| WNodePath
class 的定义如下:
public class WNodePath implements Path<WNode, WNodePath > { .. }
路径接口是 Comparable
,这是否意味着 WNodePath
也是 Comparable ,这意味着我可以在优先级队列中使用它 compareTo()
功能?
当您声明 FindPath
引用时,您不使用 extends
,它用于在 类 上提供边界(限制),用于该类型参数是合法的. (当你有一个通配符类型参数时,你可以使用扩展,但这与这里无关,参见https://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html)
直接说:
private FindPath<WNode,WNodePath> findPath = new FindPath<>();
编译。
是的,WNodePath
实现了 Comparable
,通过 Path
传递。