找不到符号-方法(通用方法) [英] Can't find symbol - method (generic method)

查看:620
本文介绍了找不到符号-方法(通用方法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两节课。
一个是短语类,

I have two classes. One is a Phrase class,

import java.util.List;
import java.util.ArrayList;

@SuppressWarnings("unchecked")
public class Phrase
{
    List<String> papers = new ArrayList();
    String name = "";
    boolean multiple = false;

    public Phrase(String name, List list)
    {
        this.name = name;
        this.papers = list;
        if(list.size() > 1)
        {
             multiple = true;
        }
    }

    public Phrase(String name, String pName)
    {
        this.name = name;
        this.papers.add(pName);
        multiple = false;
    }

    public void addPaper(String paper)
    {
        papers.add(paper);
        multiple = true;
    }

    public String getPhrase()
    {
        return name;
    }

    public List<String> getPapers()
    {
         return papers;
    }
}

另一个是KeyedLinkedList。

The Other is a KeyedLinkedList.

public class KeyedLinkedList<K,Phrase>
{
    private KeyNode first;
    private int size;

    private class KeyNode
    {
        K key;
        Phrase value;
        KeyNode previous;
        KeyNode next;

        public KeyNode(K key, Phrase value, KeyNode previous, KeyNode next)
        {
            this.key = key;
            this.value = value;
            this.previous = previous;
            this.next = next;
        }
    }

    public int size()
    {
        return size;
    }

    public boolean put(K key, Phrase val)
    {
        if(isEmpty())
        {
            first = new KeyNode(key, val, null, null);
            first.next = first;
            first.previous = first;
            size++;
            return true;
        }
        KeyNode temp = first;
        if(temp.key.equals(key))
        {

                //****ERROR LOCATION****//
            temp.value.addPaper(val.getPapers().get(0));
                //****ERROR LOCATION****//

            if(temp.value.getPapers().size() < 3)
                return false;
            return true;
        }
        temp = temp.next;
        while(temp != first)
        {
            if(temp.key.equals(key))
            {
                temp.value.addPaper(val.getPapers().get(0));
                if(temp.value.getPapers().size() < 3)
                    return false;
                return true;
            }
            temp = temp.next;
        }
        temp.previous.next = new KeyNode(key, val, temp.previous.next, first);
        first.previous = temp.previous.next;
        size++;
        return true;
    }
}

当我对此进行编译时,出现错误:可以找不到符号-方法getPapers()

When I compile this I get the error: "Can't find symbol - method getPapers()"

我显然在我的Phrase类中有getPapers()方法,而参数中的val是一个Phrase对象。我想知道我需要做什么来解决此问题。该错误发生在put方法的一半。

I obviously have the getPapers() method in my Phrase class and val in the parameters is a Phrase object. I am wondering what I need to do to fix this problem. The error occurs half way through the put method.

推荐答案

public class KeyedLinkedList<K,Phrase>
//                             ^^^^^^

这里,您在声明类型变量与 Phrase 类同名并对其进行阴影处理。任何声明为 Phrase 类型的变量都引用此类型变量,而不是 Phrase 类。

Here, you're declaring a type variable which has the same name as the Phrase class and shadows it. Any variables declared with the type Phrase refer to this type variable instead of the Phrase class.

由于我不知道您的意图是什么,所以我只能建议删除该内容。

Since I have no idea what your intent was, I can't really advise anything except to remove that.

public class KeyedLinkedList<K>

BTW,请不要这样做:

BTW, don't do this:

List<String> papers = new ArrayList();
//                        ^^^^^^^^^

这称为原始类型,这很糟糕,而且没有使用它的理由。代替 new ArrayList< String>

It's called a raw type, it's bad, and there's no reason to use it. Do new ArrayList<String> instead.

这篇关于找不到符号-方法(通用方法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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