类OBJECT中的默认构造函数是做什么的? [英] What does the default constructor in the class OBJECT do?

查看:319
本文介绍了类OBJECT中的默认构造函数是做什么的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java初学者,学习Java编译器规则如下:

I'm a Java beginner learning about Java compiler rules as below:


  1. 如果该类没有超类,请将其扩展为对象类

  2. 如果类没有构造函数,则添加默认的无参数构造函数

  3. 如果构造函数的第一行不是super( )或this(),添加super()来调用超类的默认构造函数。

我明白了我们创建的所有对象都是从超类Object派生的。
我的问题是Object类中的构造函数在调用时会做什么?

I understand that all objects we create are derived from the super class Object. My question is what does the constructor in the Object class do when called?

编辑:我的问题是关于构造函数在Object类中做了什么?我知道子类默认调用超类的构造函数。

My question is about what the constructor does in the class Object? I'm aware that subclasses call superclass's constructor by default.

例如,我有以下代码(我明确地扩展到Object并调用super()来说明什么编译器)。我的问题是,对super()的调用是做什么的?

For example, I have the following code (where I explicitly extend to Object and call super() to illustrate what the compiler does). My question is, what does the call to super() do?

public class Person extends Object
{
    private String name;
    public Person(String n)
        {   
            super();
            this.name = n;
        }
}


推荐答案


我的问题是,对super()的调用是做什么的?

My question is, what does the call to super() do?

它调用<的默认构造函数code> java.lang.Object中。并从 Java语言规范,#8.8.9


8.8.9。默认构造函数

8.8.9. Default Constructor

如果类不包含构造函数声明,则隐式声明默认构造函数。顶级类,成员类或本地类的默认构造函数的形式如下:

If a class contains no constructor declarations, then a default constructor is implicitly declared. The form of the default constructor for a top level class, member class, or local class is as follows:

默认构造函数具有与类相同的可访问性(第6.6节) )。

The default constructor has the same accessibility as the class (§6.6).

默认构造函数没有形式参数,除非在非私有内部成员类中,默认构造函数隐式声明一个形式参数,表示直接封闭的实例该类(§8.8.1,§15.9.2,§15.9.3)。

The default constructor has no formal parameters, except in a non-private inner member class, where the default constructor implicitly declares one formal parameter representing the immediately enclosing instance of the class (§8.8.1, §15.9.2, §15.9.3).

默认构造函数没有throws子句。

The default constructor has no throws clauses.

如果声明的类是原始类 Object ,那么默认构造函数有一个空体。否则,默认构造函数只调用不带参数的超类构造函数。

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

注意最后一段。

这篇关于类OBJECT中的默认构造函数是做什么的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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