我可以在Java中定义`default constructor`吗? [英] Can I define `default constructor` in Java?

查看:1067
本文介绍了我可以在Java中定义`default constructor`吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是相当理性的,但实际的。
从许多在线的Java资源,我发现一个类的默认构造函数具有以下规范:


  1. 不带参数

  2. 没有 throws >有一个空身体

Java语言规范未提供默认构造函数的定义,只声明了


如果一个类(定义)不包含构造函数声明,那么默认的
构造函数被隐式声明编译器)。


请注意,语句隐式声明意味着明确定义默认构造函数是可能的。让我们考虑下面的类:

  public class Point {
private int x;
private int y;

public int getX(){return x; }
public int getY(){return y; }
}

对于这个类,编译器将生成以下默认构造函数:

  public Point(){
super();我的问题是,如果我作为一个程序员将​​实现一个构造函数<$(


$ b c $ c> public Point(){} 可以为上面的Point调用默认构造函数如果没有,那么任何显式定义的构造函数可以被认为是一个默认构造函数

解决方案

如果您明确

strong>定义任何构造函数,则它不能是默认构造函数,即使您编写的代码与编译器生成的默认构造函数完全相同。

UPDATE:OP想要基于证据的回答

>

第13.4.12节中的编译器规则方法和构造函数声明(JLS8)


  1. 如果您在类中声明没有构造函数,则编译器生成默认构造函数。




证据:如果非内部类的源代码不包含声明的构造函数,那么将隐式声明一个没有参数的默认构造函数(§8.8.9)。





  1. 如果您声明一个或多个构造函数在你的类中,即使它是一个无参构造函数,所以类似于编译器生成的默认构造函数,你的显式构造函数将替换编译器生成的默认构造函数。要进一步强调这一点,您的显式无参构造函数等效于编译器生成的一个构造函数不是编译器生成的。




证据:向这样一个类的源代码添加一个或多个构造函数声明将阻止这个默认构造函数被隐式声明,有效地删除一个构造函数,除非一个新构造函数没有参数,默认构造函数。



My question below is rather theoretical then practical. From many Java resources available online I found out that a default constructor for a class has below specification:

  1. takes no arguments
  2. has no throws clauses
  3. has an empty body

Java language specification does not provide definition for default constructor, it only states that

If a class (definition) contains no constructor declarations, then a default constructor is implicitly declared (by a compiler).

Please notice that wording implicitly declared implies that the explicitly defined default constructor is possible. Lets consider below class:

public class Point {
  private int x;
  private int y;

  public int getX() { return x; } 
  public int getY() { return y; } 
}

For this class the compiler will generate below default constructor:

public Point() {
  super();
}

My question is, if I as a programmer would implement a constructor as public Point() { } could it be called a default constructor for above class Point? If not, then can any explicitly defined constructor be considered a default constructor? I appreciate the answer from somebody that is an expert or absolutely sure on this topic.

解决方案

If you explicitly define any constructor, then it cannot be a default constructor, even if you code one that is exactly equivalent to default constructor generated by the compiler. Default, here, means in the absence of any action by you the programmer.

UPDATE: OP wants an answer based on evidence

Compiler Rules from Section 13.4.12 Method and Constructor Declarations (JLS8):

  1. If you declare no constructors in your class, then a default constructor is generated by the compiler.

Evidence: If the source code for a non-inner class contains no declared constructors, then a default constructor with no parameters is implicitly declared (§8.8.9).

  1. If you do declare one or more constructors in your class, even if, it is a no-arg constructor and so akin to the compiler generated default constructor, your explicit constructor will replace the compiler generated default constructor. To stress this further, your explicit no-arg constructor which is equivalent to the compiler generated one is not the compiler generated one.

Evidence: Adding one or more constructor declarations to the source code of such a class will prevent this default constructor from being implicitly declared, effectively deleting a constructor, unless one of the new constructors also has no parameters, thus replacing the default constructor.

这篇关于我可以在Java中定义`default constructor`吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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