简体   繁体   English

使用泛型时无法解析方法

[英]Cannot Resolve Method when using generics

I am trying to implment a tree for my project. 我正在尝试为我的项目添加一棵树。

This tree will contain nodes which are different board states after some move. 此树将包含一些在移动后处于不同板状态的节点。

Right now my project is structured like so: 现在,我的项目的结构如下:

                               src
            Agent      Support              Test               Threes
        Tree.java      Some class.java      some class          Board.java

I want my tree to store these Board objects kept in package src.Threes. 我希望我的树将这些Board对象存储在src.Threes包中。 However in my Tree class it is not letting me access my public board methods. 但是,在我的Tree类中,这不允许我访问我的公共木板方法。

I am confident that I am just missing some really basic thing but I cannot figure this out after spending time hunting down what I am missing. 我相信我只是想念一些真正基本的东西,但是在花时间寻找我想念的东西之后,我无法弄清楚。

Please see my Tree.java code below: 请在下面查看我的Tree.java代码:

package src.Agent;


import java.util.ArrayList;
import src.Threes.Board; // unused import
import java.util.List;


public class Tree<Board> {

    private Node<Board> root;

public Tree(Board RootData){
    root = new Node<Board>(RootData);
    //root.data = RootData;
    //root.children = new ArrayList<Node<Board>>(); // maximum length of 4


}





public static class Node<Board>{
    private Board data;
    private Node<Board> parent;
    private List<Node<Board>> children;

    public Node(Board board){
        data = board;
        children = new ArrayList<Node<Board>>();

    }

    private boolean addChild(Board board){
        return !board.gameOver(); // Cannot resolve method gameOver() (public method in Board).
    }




}

public static void main(String[] args){

}

} }

This is the problem: 这就是问题:

public static class Node<Board>

That's declaring a generic class called Node , and Board is a type parameter. 那是在声明一个名为Node通用类,而Board是一个类型参数。 Within Node , the identifier Board refers to the type parameter called Board , not the type Board itself. Node ,标识符Board是指称为Board的类型参数, 而不是 Type Board本身。

It looks like you don't really want it to be generic at all, so you can just change it to: 看起来您根本不希望它是通用的,因此您可以将其更改为:

public static class Node {
    ...
}

Then change every time you use Node<Board> to just Node within the rest of the code. 然后,在每次使用Node<Board>将其更改为其余代码中的Node

Likewise, I suspect you don't want Tree to be generic. 同样,我怀疑您不希望Tree是通用的。

If you do want Tree and Node to be generic, you should declare them like this: 如果确实希望TreeNode是通用的,则应这样声明它们:

public static class Node<T> {
    private T data;
    private Node<T> parent;
    private List<Node<T>> children;
    ...
}

... but then you can't refer to Board -specific members within Node . ...但是您不能在Node引用特定于Board成员。 You could constrain T to be Board or a subtype: 您可以将T约束为Board或子类型:

public static class Node<T extends Board> {
    private T data;
    private Node<T> parent;
    private List<Node<T>> children;
    ...
}

... and at that point your gameOver etc calls will work - but that would be an odd mix of being Board-specific and generic at the same time. ……到那时,您的gameOver等调用将起作用-但这同时是特定于董事会和通用的奇怪混合。 I wouldn't expect a Node or Tree class to have any concept of "game over". 我不希望NodeTree类具有“游戏结束”的任何概念。 Separate your concerns! 分开您的担心!

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

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