在初始化时将覆盖的方法分配给超级类字段。为什么要调用子类的方法?

时间:2018-12-09 17:46:57

标签: java inheritance initialization override

我正在执行下面的代码,但没有得到基于输出的初始化顺序。

import java.util.Random;

class SuperClass{
    private int abc = this.getAnInt();
    {
        System.out.println("\nSuperClass: Inside block initializer");
        System.out.println("\tSuperClass: block Abc value: "+this.abc);
        abc = this.getAnInt();
        System.out.println("\tSuperClass: block Abc value: "+this.abc);
    }

    public SuperClass(){
        System.out.println("\nSuperClass: Inside Constructor");
        this.abc = 414;
        System.out.println("\tSuperClass: constructor Abc value: "+this.abc);
    }

    int getAnInt() {
        System.out.println("\nSuperClass: Inside getAnInt");
        System.out.println("\tSuperClass: getAnInt Abc value: "+this.abc);
        int randomInt = new Random().nextInt(21);
        System.out.println("SuperClass: getAnInt Random Int is : "+randomInt);
        return randomInt; 
    }
}

class SubClass extends SuperClass{
    private int def = this.getAnInt();
    {
        System.out.println("\nSubClass: Inside block initializer");
        System.out.println("\tSubClass: block def value: "+this.def);
        def = this.getAnInt();
        System.out.println("\tSubClass: block def value: "+this.def);
    }

    public SubClass(){
        System.out.println("\nSubClass: Inside Constructor");
        this.def = 424;
        System.out.println("\tSubClass: constructor def value: "+this.def);
    }

    @Override
    int getAnInt() {
        System.out.println("\nSubClass: Inside getAnInt");
        System.out.println("\tSubClass: getAnInt def value: "+this.def);
        int randomInt = new Random().nextInt(21);
        System.out.println("SubClass: getAnInt Random Int is : "+randomInt);
        return randomInt; 
    }
}

public class ChainingConstructor {
    public static void main(String[] args) {
        SubClass subclass = new SubClass();
        System.out.println(subclass);
    }
}

输出-----

SubClass: Inside getAnInt
    SubClass: getAnInt def value: 0
SubClass: getAnInt Random Int is : 10

SuperClass: Inside block initializer
    SuperClass: block Abc value: 10

SubClass: Inside getAnInt
    SubClass: getAnInt def value: 0
SubClass: getAnInt Random Int is : 15
    SuperClass: block Abc value: 15

SuperClass: Inside Constructor
    SuperClass: constructor Abc value: 414

SubClass: Inside getAnInt
    SubClass: getAnInt def value: 0
SubClass: getAnInt Random Int is : 6

SubClass: Inside block initializer
    SubClass: block def value: 6

SubClass: Inside getAnInt
    SubClass: getAnInt def value: 6
SubClass: getAnInt Random Int is : 18
    SubClass: block def value: 18

SubClass: Inside Constructor
    SubClass: constructor def value: 424
thinkingInJava.SubClass@368239c8

当我将SuperClass中对getAnInt()的访问权限更改为private(也删除了@Override注释)时,它似乎按我的预期工作。

  1. 静态变量和静态初始值设定项
  2. 实例变量和实例初始值设定项
  3. 构造函数

即 我通过SuperClass的私有getAnInt()得到以下输出

SuperClass: Inside getAnInt
    SuperClass: getAnInt Abc value: 0
SuperClass: getAnInt Random Int is : 1

SuperClass: Inside block initializer
    SuperClass: block Abc value: 1

SuperClass: Inside getAnInt
    SuperClass: getAnInt Abc value: 1
SuperClass: getAnInt Random Int is : 14
    SuperClass: block Abc value: 14

SuperClass: Inside Constructor
    SuperClass: constructor Abc value: 414

SubClass: Inside getAnInt
    SubClass: getAnInt def value: 0
SubClass: getAnInt Random Int is : 14

SubClass: Inside block initializer
    SubClass: block def value: 14

SubClass: Inside getAnInt
    SubClass: getAnInt def value: 14
SubClass: getAnInt Random Int is : 8
    SubClass: block def value: 8

SubClass: Inside Constructor
    SubClass: constructor def value: 424
thinkingInJava.SubClass@368239c8

我的问题是什么导致我的第一次执行调用SubClass getAnInt()方法而不是调用SuperClass getAnInt()?

0 个答案:

没有答案