创建子类的实例是否会自动创建其超类对象? [英] Does creating an instance of a child class automatically create its super class object?

查看:278
本文介绍了创建子类的实例是否会自动创建其超类对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我创建了一个子类的对象,那么是否也会创建继承子类的超类对象?如果没有,那么如何通过创建Thread类的子类(在多线程中)调用Thread类构造函数并创建一个Thread对象?

If I create an object of sub class then will the super class object also be created from which the sub class is inherited? If not then how by creating a sub class of Thread class (in multi-threading) calls the Thread class constructor and creates a Thread object?

推荐答案

子类的实例是超类的一个实例。只创建了一个对象,但作为该创建的一部分,构造函数调用一直链接在一起,直到 java.lang.Object 。例如:

An instance of the subclass is an instance of the superclass. Only one object is created, but as part of that creation, constructor calls are chained together all the way up to java.lang.Object. So for example:

public class Superclass {

    // Note: I wouldn't normally use public variables.
    // It's just for the sake of the example.
    public int superclassField = 10;

    public Superclass() {
        System.out.println("Superclass constructor");
    }
}

public class Subclass extends Superclass {

    public int subclassField = 20;

    public Subclass() {
        super(); // Implicit if you leave it out. Chains to superclass constructor
        System.out.println("Subclass constructor");
    }
}

...

Subclass x = new Subclass();
System.out.println(x instanceof Subclass);
System.out.println(x instanceof Superclass);
System.out.println(x.superclassField);
System.out.println(x.subclassField);

这个输出是:

Superclass constructor
Subclass constructor
true
true
10
20

...因为:


  • 任何构造函数所做的第一件事就是调用同一个类中的另一个构造函数,或者调用超类构造函数。所以我们在输出中的Subclass constructor之前看到Superclass constructor。

  • 我们创建的对象(显然)是 Subclass的实例

  • 我们创建的对象是 超类的实例

  • 我们创建的单个对象有两个字段( superclassField subclassField )。即使字段是私有的(通常也是这样),情况也是如此 - Subclass 中的代码将无法访问 a在超类中声明的私有字段,但该字段仍然存在 - 并且仍然可以访问超类中的代码。 / li>
  • The first thing any constructor does is call either another constructor in the same class, or a superclass constructor. So we see "Superclass constructor" before "Subclass constructor" in the output.
  • The object we've created is (obviously) an instance of Subclass
  • The object we've created is also an instance of Superclass
  • The single object we've created has both fields (superclassField and subclassField). This would be true even if the fields were private (which they usually would be) - the code in Subclass wouldn't be able to access a private field declared in Superclass, but the field would still be there - and still accessible to the code within Superclass.

事实上我们有一个单独的对象 all 状态(超类和子类)和所有行为(在 Superclass 中声明的任何方法仍可用于 Subclass ,虽然有些可能被覆盖具有更多的特殊行为,但对于理解Java的多态性方法至关重要。

The fact that we've got a single object with all the state (both superclass and subclass) and all the behaviour (any methods declared within Superclass can still be used on an instance of Subclass, although some may be overridden with more specializd behaviour) is crucial to understanding Java's approach to polymorphism.

这篇关于创建子类的实例是否会自动创建其超类对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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