我们可以有一个Java的构造函数的返回类型? [英] Can we have a return type for a constructor in Java?

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

问题描述

以下代码提供了编译错误:

The following code gives a compilation error:

class parent {
  parent(int a){}
}

class child extends parent{}

错误: / p>

Error:

Main.java:6: cannot find symbol
symbol  : constructor parent()
location: class parent
class child extends parent{}
^
1 error

我试图做不同的事情,发现添加一个返回类型的父构造函数摆脱的错误!

I was trying to do different things and found that adding a return type to the parent constructor got rid of the error!!!

class parent {
  int parent(int a){}
}

class child extends parent{}

我读过构造函数不应该有返回类型,这显然不是正确的。所以我的问题是我们应该何时返回类型的构造函数

I've read that constructors should not have return type, which clearly is not correct all the times. So my question is when should we have return type for constructor?

推荐答案

在构造函数没有返回类型



构造函数不能有返回类型。根据定义,如果方法有返回类型,它不是一个构造函数。

On constructor not having return type

Constructor must not have a return type. By definition, if a method has a return type, it's not a constructor.


JLS 8.8构造函数声明



A构造函数用于创建作为类的实例的对象。 [名称必须匹配类名,但在所有其他方面,构造函数声明看起来就像没有结果类型的方法声明。

JLS 8.8 Constructor Declarations

A constructor is used in the creation of an object that is an instance of a class. [The name must match the class name, but], in all other respects, the constructor declaration looks just like a method declaration that has no result type.






在默认构造函数



以下代码段给出编译错误:


On default constructors

The following snippet does give a compilation error:

class Parent {
   Parent(int a){}
}

class Child extends Parent{

   // DOES NOT COMPILE!!
   // Implicit super constructor parent() is undefined for default constructor.
   // Must define an explicit constructor

}

原因不是因为构造函数中有返回类型,而是因为您没有为 Child 提供 ANY 构造函数,因此 / em>构造函数由编译器自动为您创建。然而,这个默认的构造函数试图调用超类 Parent 的默认构造函数,它的默认构造函数

The reason is not because of a return type in a constructor, but because since you did not provide ANY constructor for Child, a default constructor is automatically created for you by the compiler. However, this default constructor tries to invoke the default constructor of the superclass Parent, which does NOT have a default constructor. THAT'S the source fo the compilation error.

以下是默认构造函数的规范:

Here's the specification for the default constructor:


JLS 8.8.9默认构造函数



如果类不包含构造函数声明,则不带参数的默认构造函数提供:


  • 如果要声明的类是原始类 Object 默认构造函数有一个空主体。

  • 否则,默认构造函数不带参数,只是调用没有参数的超类构造函数。

  • If the class being declared is the primordial class Object, then the default constructor has an empty body.
  • Otherwise, the default constructor takes no parameters and simply invokes the superclass constructor with no arguments.

以下是一个简单的修复:

The following is a simple fix:

class Parent {
   Parent(int a){}
}

class Child extends Parent{

   // compiles fine!
   Child() {
      super(42);
   }

}





$ b b

在与构造函数同名的方法上



以下代码片段 DOES 编译:

// COMPILES FINE!!

class Parent  {

   // method has same name as class, but not a constructor
   int Parent(int a) {
      System.out.println("Yipppee!!!");
      return 42;
   }

   // no explicit constructor, so default constructor is provided
}

class Child extends Parent {

   // no explicit constructor, so default constructor is provided

}

实际上在上面的代码片段中没有显式的构造函数。你有一个与类名称相同的常规方法。这是允许的,但不鼓励:

There is in fact no explicit constructor in the above snippet. What you have is a regular method that has the same name as the class. This is allowed, but discouraged:


JLS 8.4方法声明



A code>可以声明与 class 或成员 class 或成员相同名称的方法界面的类,,但这是不鼓励的风格

JLS 8.4 Method Declarations

A class can declare a method with the same name as the class or a field, member class or member interface of the class, but this is discouraged as a matter of style.

你会发现,如果你创建一个新Parent()或者一个 new Child()Yipppee !!!打印到标准输出。该方法在创建时不会被调用,因为它不是构造函数。

You will find that if you create a new Parent() or a new Child(), "Yipppee!!!" will NOT be printed to standard output. The method is not invoked upon creation since it's not a constructor.

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

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