使用泛型时无法解析方法 [英] Cannot Resolve Method when using generics

查看:73
本文介绍了使用泛型时无法解析方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的项目添加一棵树.

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

我希望我的树将这些Board对象存储在src.Threes包中.但是,在我的Tree类中,这不允许我访问我的公共木板方法.

I want my tree to store these Board objects kept in package src.Threes. However in my Tree class it is not letting me access my public board methods.

我有信心我只是想念一些真正的基本东西,但是在花时间寻找我想念的东西之后,我无法弄清这一点.

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.

请在下面查看我的Tree.java代码:

Please see my Tree.java code below:

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){

}

}

推荐答案

这是问题所在

public static class Node<Board>

那是在声明一个名为 Node generic 类,而 Board 是一个类型参数.在 Node 中,标识符 Board 是指称为 Board 的类型参数,不是类型是 Board 本身.

That's declaring a generic class called Node, and Board is a type parameter. Within Node, the identifier Board refers to the type parameter called Board, not the type Board itself.

您似乎根本不希望它是通用的,因此您可以将其更改为:

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 {
    ...
}

然后在每次使用 Node< Board> 时将其更改为其余代码中的 Node .

Then change every time you use Node<Board> to just Node within the rest of the code.

同样,我怀疑您不希望 Tree 是通用的.

Likewise, I suspect you don't want Tree to be generic.

如果要做希望 Tree Node 是通用的,则应这样声明它们:

If you do want Tree and Node to be generic, you should declare them like this:

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

...但是您不能在 Node 内引用特定于 Board 的成员.您可以将 T 约束为 Board 或子类型:

... but then you can't refer to Board-specific members within Node. You could constrain T to be Board or a subtype:

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

...,此时您的 gameOver 等调用将起作用-但这同时是特定于董事会和通用的奇怪混合.我不希望 Node Tree 类具有游戏结束"的任何概念.分开您的担心!

... 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. I wouldn't expect a Node or Tree class to have any concept of "game over". Separate your concerns!

这篇关于使用泛型时无法解析方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆