标签:class init end rac private abs 加载 str 赋值
1 package interfaces.music; 2 abstract class Base{ 3 Base(){ 4 print(); 5 } 6 public abstract void print(); 7 } 8 class Derived extends Base{ 9 private int i = 1; 10 public void print() { 11 System.out.println(i); 12 } 13 } 14 public class E03_Initialization { 15 public static void main(String[] args) { 16 Derived d = new Derived(); 17 d.print(); 18 } 19 }
首先寻找main函数,进入main函数;
执行 Derived d = new Derived();
加载 class Derived extends Base,发现有一个父类;
加载父类Base;
父类Base有个一构造函数,执行构造函数;
构造函数中有个print方法,此方法被子类print方法覆盖,但此时子类方法中 i=1 未被加载,因此系统默认给 i 赋值为0,故此时会执行子类print方法,打印出 0;
加载子类Derived;
执行d.print,打印出 1;
标签:class init end rac private abs 加载 str 赋值
原文地址:https://www.cnblogs.com/Qiav/p/11896770.html