当Base类构造函数调用Java中的重写方法时,Derived类对象的状态 [英] State of Derived class object when Base class constructor calls overridden method in Java

查看:157
本文介绍了当Base类构造函数调用Java中的重写方法时,Derived类对象的状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参考下面的Java代码:

Please refer to the Java code below:

class Base{
     Base(){
         System.out.println("Base Constructor");
         method();
     }
     void method(){}    
}

class Derived extends Base{
    int var = 2;
    Derived(){
         System.out.println("Derived Constructor");  
    }

     @Override
     void method(){
        System.out.println("var = "+var);
     }
 }

class Test2{
    public static void main(String[] args) {
        Derived b = new Derived();
    }
}

看到的输出是:

Base Constructor
var = 0
Derived Constructor

我认为var = 0是因为Derived对象被初始化了一半;类似于 Jon Skeet在此处说明

I think var = 0 occurs because Derived object is half initialized; similar to what Jon Skeet says here

我的问题是:

为什么如果Derived类对象尚未创建,重写的方法会被调用?

Why does the overridden method get called if the Derived class object isn't created yet?

在什么时候var赋值0?

At what point in time is var assigned value 0?

有什么用例需要这样的行为吗?

Are there any use cases where such behavior is desired?

推荐答案


  • 派生物件已创建 - 只是构造函数尚未运行。

    • The Derived object has been created - it's just that the constructor hasn't been run yet. The type of an object never changes in Java after the instant it is created, which happens before all constructors run.

      var

      var is assigned the default value of 0 as part of the process of creating an object, before constructors are run. Basically, the type reference gets set and the rest of the memory representing the object gets wiped to zero (conceptually, anyway - it may already have been wiped to zero before, as part of garbage collection)

      这种行为至少导致一致性,但它可能是一个痛苦。在一致性方面,假设你有一个可变基类的只读子类。基类可能有一个 isMutable()属性,它实际上默认为true - 但是子类覆盖它总是返回false。在子类构造函数运行之前,对象是可变的,但是在之后是不可变的。另一方面,在你的类的构造函数运行之前你最终在一个类中运行代码的情况下,这是

      This behaviour at least leads to consistency, but it can be a pain. In terms of consistency, suppose you had a read-only subclass of a mutable base class. The base class may have an isMutable() property which was effectively defaulted to true - but the subclass overrode it to always return false. It would be odd for the object to be mutable before the subclass constructor ran, but immutable afterwards. On the other hand, it's definitely strange in situations where you end up running code in a class before the constructor for that class has run :(



        一些指南:

      A few guidelines:


      • 尝试不要在一个避免这种情况的方法是在静态方法中工作,然后使静态方法的最后一部分是一个简单地设置字段的构造函数调用。当然,这意味着你不会得到多态的好处,而你

      • Try not to do much work in a constructor. One way of avoiding this is to do work in a static method, and then make the final part of the static method a constructor call which simply sets fields. Of course, this means you won't get the benefits of polymorphism while you're doing the work - but doing so in a constructor call would be dangerous anyway.

      尽量避免在构造函数中调用非final方法 - 很有可能造成混淆。记录任何方法调用你真正 清楚地做,以便任何覆盖他们知道他们将在初始化完成之前调用。

      Try very hard to avoid calls to non-final methods during a constructor - it's very likely to cause confusion. Document any method calls you really have to make very clearly, so that anyone overriding them knows that they will be called before initialization has finished.

      如果您在建设过程中必须调用方法,则通常

      If you have to call a method during construction, it's usually not then appropriate to call it afterwards. If that's the case, document it and attempt to indicate it in the name.

      尝试不首先过度使用继承 - 这只会变成一个问题,当你有一个子类派生自一个超类而不是Object :)设计继承是棘手的。

      Try not to overuse inheritance in the first place - this is only going to become an issue when you've got a subclass deriving from a superclass other than Object :) Designing for inheritance is tricky.

      这篇关于当Base类构造函数调用Java中的重写方法时,Derived类对象的状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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