0

尝试编译时,我的代码无法编译:

package ch02.genericStringLogs;

public class DemoGenericLogs {
  public static void main(String[] args) {
    GenericLogInterface<Float> genFloatLog = new LinkedGenericLog<Float>();
    LLGenericNode<Float> node0 = new LLGenericNode<Float>(2.2);
    LLGenericNode<Float> node1 = new LLGenericNode<Float>(3.3);
    LLGenericNode<Float> node2 = new LLGenericNode<Float>(4.4);
    LLGenericNode<Float> node3 = new LLGenericNode<Float>(5.5);
    genFloatLog.insert(node0);
    genFloatLog.insert(node1);
    genFloatLog.insert(node2);
    genFloatLog.insert(node3);

    System.out.println(genFloatLog.size());
    System.out.println(genFloatLog.toString());
    genFloatLog.clear();
    System.out.println(genFloatLog.size());

    GenericLogInterface<String> genStringLog = new LinkedGenericLog<String>();
    LLGenericNode<String> string0 = new LLGenericNode<String>("one");
    LLGenericNode<String> string1 = new LLGenericNode<String>("two");
    LLGenericNode<String> string2 = new LLGenericNode<String>("three");
    LLGenericNode<String> string3 = new LLGenericNode<String>("four");

    System.out.println(genStringLog.size());
    System.out.println(genStringLog.toString());
    genStringLog.clear();
    System.out.println(genStringLog.size());
  }
}

我收到此错误:

Error:
    part1/ch02/genericStringLogs/DemoGenericLogs.java:5: cannot find symbol
    symbol  : constructor LinkedGenericLog()
    location: class ch02.genericStringLogs.LinkedGenericLog<java.lang.Float>
4

2 回答 2

3

假设它与您之前的问题中的类相同,那么唯一的构造函数LinkedGenericLog<T>就是这个:

public LinkedGenericLog(String name)

因此,当您构建一个时,您需要传入一个名称。例如:

GenericLogInterface<Float> genFloatLog = new LinkedGenericLog<Float>("Some name");

如果您不想传入名称,则需要进行更改LinkedGenericLog- 添加无参数构造函数。在这种情况下,您希望日志具有什么名称?

于 2013-02-27T22:45:34.433 回答
1

这条线...

GenericLogInterface<String> genStringLog = new LinkedGenericLog<String>();

表示您正在尝试调用无参数构造函数。

如果您遇到该错误,您的LinkedGenericLog类不能有无参数构造函数。Java默认提供了一个,除非您定义了其他接受参数的构造函数。

于 2013-02-27T22:44:50.443 回答