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

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

问题描述

Oracle Java教程网站有这段让我感到困惑的是:

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


所有类都至少有一个
构造函数。如果一个类不
显式声明任何,Java
编译器自动提供一个
无参构造函数,称为
默认构造函数。这个默认的
构造函数调用父类的
无参构造函数,如果该类没有其他
父构造函数,则调用Object
构造函数。如果父类没有
构造函数(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?

推荐答案


如果所有对象都直接或间接继承from 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 li>
  • 没有明确声明一个无参数的构造函数(例如 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 类中的c $ c> 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编译器插入一个隐式调用到超类no-args constructor ...并且如果no-args构造函数不存在或对子类不可见,则会导致编译错误。)

(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?

不直接。但是,声明构造函数private将阻止从子类调用构造函数。

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

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

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