在java中使用默认构造函数,即使参数化构造函数存在 [英] Using default Constructors in java, even if the parameterized constructors are present

查看:436
本文介绍了在java中使用默认构造函数,即使参数化构造函数存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想在这里清除我的概念,所以我问...

I just wanted to clear my concept here, so i am asking...

如果我为我的类定义一个显式的参数化构造函数,我仍然可以调用默认构造函数由java编译器提供(默认为每个类提供)

If I define an explicit parameterized constructor for my class, can I still invoke the default constructor provided by the java compiler (which is provided for every class by default) ??

或者是否会导致编译时错误在这种情况下?
请解释一下相对于编译器调用发生了什么!

Or does it cause result in a compile time error in such case?? Please explain what exactly happens with respect to the calls made by the compiler !!

推荐答案

当且仅当没有提供构造函数时,将创建一个无参数构造函数编译器。

If and only if no constructor is provided, then a no-argument constructor is created by the compiler.

JLS在第8章


如果类不包含构造函数声明,没有正式参数的构造函数和没有throws子句被隐式声明。

If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.

一个catch是:


如果默认构造函数被隐式声明,但超类没有可访问的构造函数(§6.6),这是一个编译时错误,无参数,没有throw子句。

It is a compile-time error if a default constructor is implicitly declared but the superclass does not have an accessible constructor (§6.6) that takes no arguments and has no throws clause.

这篇关于在java中使用默认构造函数,即使参数化构造函数存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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