[原]加深对继承的理解

[原]加深对继承的理解

首先看看这这个程序,试试写出输出的结果
public class Parent
{
 public void test()
 {}

 public Parent()
 {
  test();
 }

 public static void main(String[] args)
 {
  new Child();
 }
}

class Child extends Parent
{
 private int instanceValue = 20;
 public void test()
 {
  System.out.println("instance value is: " + instanceValue);
 }
}

/***********************output is*********************************/
instance value is: 0
/*****************************************************************/

结果是不是出乎你的预料呢?

如果把程序改为这样:
public class Parent {
 public void test() {
 }

 public Parent() {
  test();
 }

 public static void main(String[] args) {
  new Child();
 }
}

class Child extends Parent {
 private int instanceValue = 20;

 Child() {
  super();
  test();
 }

 public void test() {
  System.out.println("instance value is: " + instanceValue);
 }
}

/***********************output is*********************************/
instance value is: 0
instance value is: 20
/*****************************************************************/
是不是明白了一点,好,看看原来的程序,首先执行到Child的构造函数,它会调用Parent的构造函数,接着它会调用重写的test,执行
System.out.println("instance value is: " + instanceValue);
因为这里,instanceValue并没有初始化,所以输出结果是instance value is:0

看明白了这个,再来看看一段Think in java的程序
//: Beetle.java
// The full process of initialization.

class Insect {
 int i = 9;

 int j;

 Insect() {
  prt("i = " + i + ", j = " + j);
  j = 39;
 }

 static int x1 = prt("static Insect.x1 initialized");

 static int prt(String s) {
  System.out.println(s);
  return 47;
 }
}

public class Beetle extends Insect {
 int k = prt("Beetle.k initialized");

 Beetle() {
  prt("k = " + k);
  prt("j = " + j);
 }

 static int x2 = prt("static Beetle.x2 initialized");

 static int prt(String s) {
  System.out.println(s);
  return 63;
 }

 public static void main(String[] args) {
  prt("Beetle constructor");
  Beetle b = new Beetle();
  prt("x1="+Insect.x1);
  prt("x2="+Beetle.x2);
 }
} // /:~

这个貌似比刚才的复杂一点,我觉得主要难点是在static变量的初始化处,于是我加上了
prt("x1="+Insect.x1);
prt("x2="+Beetle.x2);
这两句话,希望通过结果能帮助您理解

如果您仔细分析,一定能够得出结果,有任何疑问可以留言,我把输出结果放在下面,在自己写出结果前千万别看~~

/***********************output is*********************************/
static Insect.x1 initialized
static Beetle.x2 initialized
Beetle constructor
i = 9, j = 0
Beetle.k initialized
k = 63
j = 39
x1=47
x2=63
/*****************************************************************/

One thought on “[原]加深对继承的理解

  1. wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling
    wow power leveling -228974778789049

Leave a Reply

Your email address will not be published. Required fields are marked *