在 java.io.File 中找不到构造函数 File()
这可能是显而易见的,所以请耐心听我说。
是的,我知道 java.io.File 没有默认构造函数。
问题是,当我尝试扩展 java.io.File 时,它显示“无法在 java.io.File 中找到构造函数 File()”,即使我覆盖了 java.lang.Object 中的默认构造函数。
这是我的代码:
AbsRelFile.java
import java.io.File;
public class AbsRelFile extends File {
File f;
private AbsRelFile(){
}
}
这给了我一个错误,即使我重写了构造函数。
注意:本课程尚未完成。不要评论为什么我不需要这个或评论这个类如何无用。在出现此错误之前我刚刚开始编写它。
This is probably obvious, so bear with me.
YES, I KNOW THAT java.io.File has no default constructor.
The problem is that When I try to extend java.io.File, it says "Cannot find constructor File() in java.io.File" even though I am overriding the default constructor in java.lang.Object.
Here is my code:
AbsRelFile.java
import java.io.File;
public class AbsRelFile extends File {
File f;
private AbsRelFile(){
}
}
This gives me an error, even though I am overriding the constructor.
NOTE: This class is not finished. Don't make a comment about why wouldn't I need this or a comment about how this class is useless. I just started writing it before I got this Error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
因为您没有在默认构造函数中显式调用 super(...) ,所以它隐式地尝试调用超类的默认构造函数,正如您所指出的,它不会在这种情况下不存在(super 是一个
File
的情况)。问题的解决方案是在默认AbsRelFile()
构造函数中调用超级构造函数。如果您不想为您的类提供默认构造函数,则需要使用一些默认值调用super(...)
。Because you didn't make an explicit call to
super(...)
in your default constructor, it is implicitly attempting to call the default constructor for the super class, which, as you point out, doesn't exist in this case (the case of super being aFile
). The solution to your problem is to make a call to the super constructor in your defaultAbsRelFile()
constructor. If you wan't to provide a default constructor for your class, you're going to need to callsuper(...)
with some default values.当您定义构造函数时,Java 会插入对超级构造函数的隐式调用作为构造函数的第一行。所以你的构造函数相当于:
由于超类
File
中没有默认构造函数,所以会报错。要解决此问题,您需要在第一行显式调用超类构造函数:最有可能的是,您还必须为
AbsRelFile
构造函数定义一些合适的参数,您可以将其传递给 super称呼。另一方面,构造函数不能被重写。因此,说您正在重写
Object
类构造函数是错误的。您只需为AbsRelFile
类定义一个构造函数。When you define a constructor, Java inserts an implicit call to the super constructor as the very first line of the constructor. So your constructor is equivalent to:
Since there is no default constructor in the super class
File
, it gives an error. To fix this, you need to place an explicit call to the super class constructor as the first line:Most probably, you'll have to define some suitable parameters for
AbsRelFile
constructor too which you can pass to super call.On another note, constructors cannot be overridden. So it is wrong to say that you're overriding the
Object
class constructor. You're simply defining a constrcutor forAbsRelFile
class.如果您自己不调用超级构造函数,构造函数默认会调用超类的默认构造函数。
为了避免这种情况,请调用 File 的实际定义的构造函数。
Constructors, by default, call the default constructor of the super-class if you don't make a super constructor call yourself.
To avoid this, make a call to an actually-defined constructor of File.
Java 会自动在空构造函数中调用 super(),这就是您收到错误的原因。
Java automatically puts in a call to super() in your empty constructor, which is why you get the error.
问题是您的
AbsRelFile
构造函数尝试调用File
的无参数构造函数。您所编写的内容相当于您需要确保您显式调用确实存在的
File
构造函数之一。例如:显然,您需要找出一个安全/无害/适当的超类构造函数和参数以用于您的特定用例。 (我不知道
AbsRefFile
到底应该是什么......所以我无法就此提供建议。)另外 - 你不会“覆盖”构造函数。 Java 中的构造函数永远不会被继承,因此重写在这里不适用。相反,您在子类中声明构造函数,并通过显式
super(...)
调用或 Java 默认插入的隐式调用将它们链接到直接超类中的相应构造函数。The problem is that your
AbsRelFile
constructor is trying to call the no-args constructor ofFile
. What you have written is equivalent toYou need to make sure that you explicitly invoke one of the
File
constructors that does exist. For example:Obviously, you will need to figure out a safe / harmless / appropriate superclass constructor and arguments to use for your particular use-case. (I haven't a clue what an
AbsRefFile
is really supposed to be ... so I cannot advise on that.)Aside - you don't "override" constructors. Constructors are never inherited in Java, so overriding simply doesn't apply here. Instead you declare constructors in the subclass, and have them chain to the appropriate constructors in the immediate superclass via an explicit
super(...)
call ... or the implicit one the Java inserts by default.首先,我希望您的字段“File f”与尝试访问超类无关,而是与“Rel”或“Abs”有关。
其他海报已正确指出您的隐式默认构造函数(AbsRelfile ()) 将尝试调用 super() - 它不存在。所以唯一的解决方案是创建一个构造函数来传递一些有效的参数。
如果您尝试“包装”整个 java.util.File 类(例如在创建自己的异常时),您可能应该为每个原始构造函数提供一个包装器。像 Eclipse 这样的现代 IDE 应该可以通过右键单击来实现。
请注意,File 并不要求给定的文件名存在,特别是当您想要执行诸如 file.mkdir() 之类的操作时,该文件名不存在。
如果您需要一个实际的临时文件来使用,您总是可以执行以下操作:
.. 但我很困惑为什么您要首先对 File 进行子类化。
First of all, I hope your field "File f" is not related to trying to access the superclass, but something to do with 'Rel' or 'Abs'..
The other posters have correctly pointed out that your implicit default constructor (AbsRelfile()) will attempt to call super() - which does not exist. So the only solution is to make a constructor that passes down some valid arguments.
If you are attempting to 'wrap' the whole java.util.File class (like when making your own Exception) you should probably provide a wrapper for each of the original constructors. Modern IDEs like Eclipse should have this a right-click away.
Note that File does not require that the given filename exists, in particular it does not exist when you want to do operations like file.mkdir().
If you need an actual, temporary file to work with, you could always do something like:
.. but I'm puzzled as to why you want to subclass File in the first place.
用一行解释为什么:
当您定义带有参数的构造函数(如在 File 类中)时,Java 编译器不会为您生成默认构造函数。
To explain WHY in one line:
When you define a constructor with a parameter (as in the File class), Java compiler WILL NOT generate the default constructor for you.