简体   繁体   English

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

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

i have this class:我有这个 class:

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

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

and the path interface:和路径接口:

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

where T, P and N are generic types.其中 T、P 和 N 是泛型类型。 i want to define a variable FindPath in another class, and call it's constructor, basically i want something like this:我想在另一个 class 中定义一个变量 FindPath,并调用它的构造函数,基本上我想要这样的东西:

public class TestD {

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

} 

( WNode,WNodePath are another classes i use in the generic types ) WNode,WNodePath是我在泛型类型中使用的另一个类)

what am i doing wrong?我究竟做错了什么? how to do this right?如何做到这一点?

also another question:||还有一个问题:|| the WNodePath class is defined like so: WNodePath class 的定义如下:

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

the path interface is Comparable , does that mean that also WNodePath is Comparable, which means that i can use it compareTo() function in a priority queue?路径接口是Comparable ,这是否意味着WNodePath也是 Comparable ,这意味着我可以在优先级队列中使用它compareTo() function 吗?

When you declare a FindPath reference you don't use extends , that's used to provide bounds (limits) on the classes it is legal to use for that type parameter.当您声明FindPath引用时,您不使用extends ,它用于为该类型参数合法使用的类提供边界(限制)。 (You can use extends when you have a wildcard type parameter, but that's not relevant here, see https://docs.oracle.com/javase/tutorial/extra/generics/wildcards.html ) (当您有通配符类型参数时,您可以使用扩展,但这与此处无关,请参阅https://docs.oracle.com/javase/tutorial/extra/generics/wildcards.ZFC35EZ8830D5FC69D807BADE5Z

Just say:说啊:

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

Which compiles.哪个编译。

Yes, WNodePath implements Comparable , transitively via Path .是的, WNodePath实现了Comparable ,通过Path传递。

暂无
暂无

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

相关问题 在Java中定义扩展类的属性 - Define an attribute that extends a class in java Java-扩展一个类的类,并具有扩展另一个类的组件的组件 - Java - Class that extends a class, and has a component that extends the other class' component 当 class 扩展 LinearLayout 时,如何将 class 作为 Activity 调用 - How to call class as an Activity when class has extends LinearLayout 我们如何定义一个扩展java.lang类的Exception类的类 - How can we define a class which extends the Exception class of java.lang class How to change class field type and layout when we define a new class by extending a class in Android Java? - How to change class field type and layout when we define a new class by extending a class in Android Java? 将 class 类型的变量分配给扩展声明的变量类型的不同 class - Assigning a variable of type class to a different class that extends the declared variable type java如何从扩展线程类获取变量值 - java how to get variable value from extends thread class 如何验证泛型在Java中扩展了另一个类/接口? - How to verify a generic type extends another class/interface in Java? 如何满足参数类型Class <? extends someInterface> 在java中 - How to satisfy parameter type Class<? extends someInterface> in java 如何创建具有类型参数的泛型类的实例,该类在其自己的类中扩展了静态类? - How to create an instance of a generic class that has a type paramater which extends a static class within its own class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM