Java 类如何没有无参数构造函数? [英] How can a Java class have no no-arg constructor?

查看:28
本文介绍了Java 类如何没有无参数构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Oracle Java 教程站点中有一段让我感到困惑:

The Oracle Java tutorial site has this paragraph that is confusing me:

所有班级至少有一个构造函数.如果一个班级没有显式声明任何,Java编译器自动提供一个无参数构造函数,称为默认构造函数.这个默认构造函数调用父类的无参数构造函数,或 Object如果类没有其他构造函数父母.如果家长没有构造函数(对象确实有一个),编译器将拒绝该程序.

All classes have at least one constructor. If a class does not explicitly declare any, the Java compiler automatically provides a no-argument constructor, called the default constructor. This default constructor calls the class parent's no-argument constructor, or the Object constructor if the class has no other parent. If the parent has no constructor (Object does have one), the compiler will reject the program.

如果所有对象直接或间接从 Object 继承,怎么可能引起所说的编译器拒绝?这与构造函数是私有的有关吗?

If all objects directly or indirectly inherit from Object how is it possible to elicit the compiler rejection spoken of? Does it have to do with the constructor being private?

推荐答案

如果所有对象直接或间接从 Object 继承,怎么可能引起所说的编译器拒绝?

If all objects directly or indirectly inherit from Object how is it possible to elicit the compiler rejection spoken of?

我认为您误解的基础是您认为构造函数是继承的.事实上,构造函数在 Java 中不是继承的.因此,请考虑以下示例:

I think the basis is of your misunderstanding is that you are thinking that constructors are inherited. In fact, constructors are NOT inherited in Java. So consider the following example:

public class A {
    public A(int i) { super(); ... }
}

public class B extends A {
    public B() { super(); ... }
}

A 类:

  • 不从Object继承任何构造函数,
  • 未明确声明无参数构造函数(即 public A() {...}),并且
  • 没有默认构造函数(因为它确实声明了另一个构造函数).
  • does not inherit any constructors from Object,
  • does not explicitly declare a no-args constructor (i.e. public A() {...}), and
  • does not have a default constructor (since it does declare another constructor).

因此,它只有一个构造函数:public A(int).

Hence, it has one and only one constructor: public A(int).

B 类中对 super() 的调用尝试使用 A 中不存在的无参数构造函数,并给出一个编译错误.要解决此问题,您需要更改 B 构造函数以使用 A(int) 构造函数,或者在 A 中声明一个显式的无参数构造函数代码>.

The call to super() in the B class tries to use a non-existent no-args constructor in A and gives a compilation error. To fix this, you either need to change the B constructor to use the A(int) constructor, or declare an explicit no-args constructor in A.

(顺便说一句,构造函数没有必要显式调用超类构造函数......就像我所做的那样.但是很多人认为包含显式调用是一种很好的风格. 如果您省略它,Java 编译器会插入对超类无参数构造函数的隐式调用......如果无参数构造函数不存在或对子类不可见,则会导致编译错误.)

(Incidentally, it is not necessary for a constructor to explicitly call a superclass constructor ... as I've done. But a lot of people think it is good style to include an explicit call. If you leave it out, the Java compiler inserts an implicit call to the superclasses no-args constructor ... and that results in a compilation error if the no-args constructor does not exist or is not visible to the subclass.)

这与构造函数是私有的有关吗?

Does it have to do with the constructor being private?

不是直接的.但是,将构造函数声明为私有将阻止从子类调用该构造函数.

Not directly. However, declaring a constructor private will prevent that constructor being called from a child class.

这篇关于Java 类如何没有无参数构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆