LunchBag<Lunch> lunchBox = new LunchBag<>();
上面的声明给我一个错误,“错误:找不到符号”,指的是LunchBag,尽管LunchBag被定义为实现GeneralBag接口的类。这是具体的错误:
error: cannot find symbol
LunchBag<Lunch> lunchBox = new LunchBag<>();
symbol: class LunchBag
location: class TestLunchBag
对于同一行,以上代码段重复两次,表示存在两个错误。
我定义了一个午餐类,其中只有一个int字段用于表示午餐项目的代码(1个代表苹果,2个代表三明治)。在我对LunchBag的类定义中
package arraybags;
import java.util.Arrays;
public class LunchBag<T> implements GeneralBagInterface<T> {
private T[] bag;
private static final int DEFAULT_CAPACITY = 10;
private int numberOfEntries;
/**
* Creates an empty bag whose initial capacity is 10.
*/
public LunchBag() {
this(DEFAULT_CAPACITY);
}
/**
* Creates an empty bag having a given initial capacity.
*
* @param capacity the integer capacity desired
*/
public LunchBag(int capacity) {
numberOfEntries = 0;
bag = (T[]) new Object[capacity];
}}
我的TestLunchBag类的主要方法和显示错误的行如上所述:
LunchBag<Lunch> lunchBox = new LunchBag<>();
TestLunchBag类代码:
package arraybags;
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class TestLunchBag{
public static void main(String[] args)
{
LunchBag<Lunch> lunchBox = new LunchBag<>();
Scanner in = new Scanner(System.in);
choice = in.nextInt();
System.out.println("Enter lunch item (ID):");
int userLunchItemEntry = in.nextInt();
Lunch lunch = new Lunch(userLunchItemEntry);
if (lunch.isValidValue == true)
{
lunchBox.add(lunch);
}
}}
明智的文件目录:我有一个名为arraybags的软件包,该软件包具有:
GeneralBagInterface.java
Lunch.java
LunchBag.java
TestLunchBag.java
我的界面的import和def语句:
package arraybags;
public interface GeneralBagInterface <T>{
//general bag operation methods that I override in my LunchBag class
}
如果需要的话,午餐课的定义dec和以下软件包:
package arraybags;
public class Lunch {
private int lunchItem;
public boolean isValid = false;
//and relevant methods
}
我是在声明错误消息还是默认构造函数无效/编写不正确?如果是这样,我将如何正确声明一个新的LunchBag对象,该对象将Lunches对象纳入其bag数组字段?
答案 0 :(得分:1)
LunchBag
已编译,并且已导入到您的testLunchBag
类中(如果它们不在同一个程序包中)。
答案 1 :(得分:0)
您的LunchBag
类定义似乎缺少结尾的“}”。