我有一个主类 DataManager,它有一个扩展 DataManager 的子类 FileHandler。
public class DataManager{
protected File file;
private FileHandler fileHandler;
public DataManager(File fileIn) {
this.file = fileIn;
fileHandler = new FileHandler(file);
}
//other class stuff}
其他类:
public class FileHandler extends DataManager {
private File file;
public FileHandler() {
this.file = file;
}
//other class stuff }
我收到一条错误消息,指出构造函数无法应用于给定类型。这是我第一次在 Java 中处理继承问题,这个问题对我想要的东西没有太大帮助。
这是我从 NetBeans 获得的更具体的版本...
“类 DataManager 中的构造函数 DataManager 不能应用于给定类型; 要求:文件 发现:没有参数 原因:实际和形式参数列表的长度不同 "
最佳答案
FileHandler() 正在隐式调用其父级的构造函数:super()。
super() 期望找到一个不带参数的构造函数。但是,父级上只有 1 个构造函数需要一个文件。
引用以下内容:
修复你的错误调用:super(file)
您的代码应该是:
import java.io.File;
public class DataManager {
protected File file;
public DataManager(File fileIn) {
this.file = fileIn;
}
}
// -------------
import java.io.File;
public class FileHandler extends DataManager {
public FileHandler(File file) {
super(file);
}
}
关于给定类型不能应用 Java 构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26937823/