为什么项目的一个类中的方法不能识别另一个类的方法?

时间:2015-06-20 04:37:52

标签: java

我正在尝试使用Eclipse创建一个Java程序,该程序大致模拟了一个拥有多个人或角色及其交互的小城镇。

我添加了一个类字符 - 它有一个方法 - meet(character)。但是,当我尝试使用另一个类中的字符对象 meet(character) - location - 在Eclipse中的同一个包和项目中调用时,会导致错误:对于类型字符,方法meet(character)是未定义的。

我的第一个代码块是我的 location 类。它仅包含 add 方法。

第二块代码是我的字符类,仅包括 meet(字符) getID()方法

"dev": "concurrently --kill-others \"npm run start-watch\" \"npm run wp-server\""

第二块代码,带有 meet(character)方法的字符

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

public class location<character> implements List<character> {

private String name;
private ArrayList<character> occupants;
private boolean professional;
private int cost;
private character owner;
private int hours;

public location(character o,  boolean prof, String n, int c, int h)
{
    name = n;
    professional = prof;
    cost = c;
    owner = o;
    hours = h;

}
//Methods other than add(character c) have been excluded
public boolean add(character c)
{
    for(character a:occupants)
    {
        //This if statement is the part with errors. 
        //Error message: The method meet(character) is undefined for the type character
        if(c.meet(a))
        {
            a.meet(c);
        }
    }
    return occupants.add(c);
}
}

非常感谢任何帮助,如果您需要我提供更多我的代码,请询问。我已经找到了像我这样的问题,但我还没有找到一种方法来说明它会带来相关结果。

1 个答案:

答案 0 :(得分:0)

问题在于:

class location<character> implements List<character>
               ^--here--^

在这种情况下,通用名称为character,从而在代码中产生问题。您需要摆脱该通用定义并使用它:

class location implements List<character>

事实上,您甚至不需要location类来实现List<character>,只需将其用作List<character>的包装:

public class location<character> {

    private List<character> occupants;
    //...

}

最后但同样重要的是,我强烈建议您关注Naming Conventions for Java。这样,按locationLocationcharacter或类似名称重命名Char的{​​{1}}和NPC