class 中的构造函数不能应用于给定类型 - 摘要 Class
Constructor in class cannot be applied to given types - Abstract Class
我知道名义上的问题已经在这里和那里得到了解答,但是每一个答案仍然不能解决我的问题。问题是这样的:
我有一个抽象 class,它是节点,包含这样的构造函数:
public Node(List<Record> dataSet, int labelIndex) {
this.allSamples = new ArrayList<Integer>();
this.dataSet = dataSet;
this.classificationLabelIndex = labelIndex;
this.entropy = 0;
}
然后,我将抽象 class 扩展到 TreeNode,包含这样的构造函数(使用 super):
public TreeNode(List<Record> dataSet, int labelIndex, List<Attribute> attributes, int level, double threshhold) {
super(dataSet, labelIndex);
this.attributes = attributes;
splittedAttrs = new HashSet<Integer>();
this.level = level;
this.displayPrefix = "";
this.children = null;
this.threshhold = threshhold;
}
因此,TreeNode class 扩展了抽象节点 class 并使用超级方法从节点 class 调用数据集和标签索引,但随后我收到警告 "constructor Node in class Node cannot be applied to given types, required no arguments." 也许因为我在TreeNode中添加了一些参数,但我仍然认为这不太可能。任何帮助表示赞赏。
没有看到更多细节,很难知道问题出在哪里。不,你不能直接实例化一个抽象 class,但是你可以从子 class 对抽象 class 的构造函数调用 super,所以你所做的似乎是美好的。一个想法是确保您与另一个名为 Node 或 TreeNode 的 class 没有任何冲突的 class 路径问题。实际上,您创建的自定义 class 可能已经存在于您的 class 路径中,如果您导入了这些,例如:
https://docs.oracle.com/javase/7/docs/api/javax/swing/tree/TreeNode.html
https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Node.html
尝试将它们重命名为更适合您的名称,例如 MyTreeNode 和 MyNode 或其他名称。
无论如何,我都尝试尽可能地复制您发送的代码,但在我这边没有发现任何问题(即与我的 class 路径中的其他导入没有冲突)。检查一下,看看它是否与您拥有的相匹配。如果没有,请 copy/paste 错误的堆栈跟踪以及所有代码。谢谢
import java.util.ArrayList;
import java.util.List;
import java.util.ArrayList;
import java.util.List;
public abstract class Node {
private List<Integer> allSamples;
private List<Record> dataSet;
private int classificationLabelIndex;
private int entropy;
public Node(List<Record> dataSet, int labelIndex) {
this.allSamples = new ArrayList<Integer>();
this.dataSet = dataSet;
this.classificationLabelIndex = labelIndex;
this.entropy = 0;
}
}
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class TreeNode extends Node {
private List<Attribute> attributes;
private Set<Integer> splittedAttrs;
private int level;
private String displayPrefix;
private Object children;
private double threshhold;
public TreeNode(List<Record> dataSet, int labelIndex,
List<Attribute> attributes, int level, double threshhold) {
super(dataSet, labelIndex);
this.attributes = attributes;
splittedAttrs = new HashSet<Integer>();
this.level = level;
this.displayPrefix = "";
this.children = null;
this.threshhold = threshhold;
}
public static void main(String[] args) {
Node node = new TreeNode(new ArrayList<Record>(), 1,
new ArrayList<Attribute>(), 1, 1.1);
}
}
class Record {}
class Attribute {}
我知道名义上的问题已经在这里和那里得到了解答,但是每一个答案仍然不能解决我的问题。问题是这样的:
我有一个抽象 class,它是节点,包含这样的构造函数:
public Node(List<Record> dataSet, int labelIndex) {
this.allSamples = new ArrayList<Integer>();
this.dataSet = dataSet;
this.classificationLabelIndex = labelIndex;
this.entropy = 0;
}
然后,我将抽象 class 扩展到 TreeNode,包含这样的构造函数(使用 super):
public TreeNode(List<Record> dataSet, int labelIndex, List<Attribute> attributes, int level, double threshhold) {
super(dataSet, labelIndex);
this.attributes = attributes;
splittedAttrs = new HashSet<Integer>();
this.level = level;
this.displayPrefix = "";
this.children = null;
this.threshhold = threshhold;
}
因此,TreeNode class 扩展了抽象节点 class 并使用超级方法从节点 class 调用数据集和标签索引,但随后我收到警告 "constructor Node in class Node cannot be applied to given types, required no arguments." 也许因为我在TreeNode中添加了一些参数,但我仍然认为这不太可能。任何帮助表示赞赏。
没有看到更多细节,很难知道问题出在哪里。不,你不能直接实例化一个抽象 class,但是你可以从子 class 对抽象 class 的构造函数调用 super,所以你所做的似乎是美好的。一个想法是确保您与另一个名为 Node 或 TreeNode 的 class 没有任何冲突的 class 路径问题。实际上,您创建的自定义 class 可能已经存在于您的 class 路径中,如果您导入了这些,例如: https://docs.oracle.com/javase/7/docs/api/javax/swing/tree/TreeNode.html https://docs.oracle.com/javase/7/docs/api/org/w3c/dom/Node.html
尝试将它们重命名为更适合您的名称,例如 MyTreeNode 和 MyNode 或其他名称。
无论如何,我都尝试尽可能地复制您发送的代码,但在我这边没有发现任何问题(即与我的 class 路径中的其他导入没有冲突)。检查一下,看看它是否与您拥有的相匹配。如果没有,请 copy/paste 错误的堆栈跟踪以及所有代码。谢谢
import java.util.ArrayList;
import java.util.List;
import java.util.ArrayList;
import java.util.List;
public abstract class Node {
private List<Integer> allSamples;
private List<Record> dataSet;
private int classificationLabelIndex;
private int entropy;
public Node(List<Record> dataSet, int labelIndex) {
this.allSamples = new ArrayList<Integer>();
this.dataSet = dataSet;
this.classificationLabelIndex = labelIndex;
this.entropy = 0;
}
}
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class TreeNode extends Node {
private List<Attribute> attributes;
private Set<Integer> splittedAttrs;
private int level;
private String displayPrefix;
private Object children;
private double threshhold;
public TreeNode(List<Record> dataSet, int labelIndex,
List<Attribute> attributes, int level, double threshhold) {
super(dataSet, labelIndex);
this.attributes = attributes;
splittedAttrs = new HashSet<Integer>();
this.level = level;
this.displayPrefix = "";
this.children = null;
this.threshhold = threshhold;
}
public static void main(String[] args) {
Node node = new TreeNode(new ArrayList<Record>(), 1,
new ArrayList<Attribute>(), 1, 1.1);
}
}
class Record {}
class Attribute {}