2013年11月14日星期四

the java class initialization problem

package test;

public class A {

public A () {
System.out.println ("this is a empty constructor");
}

public A (int i) {
System.out.println ("this is unempty constructor");
}

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

}

class B extends A {

public B () {
this (1);
}

public B (int i) {
super (3);
System.out.println ("hah");
}


}
This is a very magical problems in the subclass constructor method , you must call the parent class constructor ( if you do not write, default empty method call ) , then the problem is now how to explain it ? · · · · B class constructor inside a hollow does not call the parent class constructor , new B () , it should first call the parent class constructor , then call the subclass constructor method, then a class should call the no-argument constructor , right ? · · · and B class constructor executes, and then run B class constructor method, then this time, they should call the parent class constructor super (3). This is obviously wrong, but in the end how to explain it ? · ·
------ Solution ------------------------------------ --------
java syntax restricts a constructor in the same can not be simultaneously super ( parameter list ) and this ( parameter list )

public B () {
this (1);
}

This code does not call super ()

So your code execution order

this (1);
super (3);
public A (int i) {
System.out.println ("this is unempty constructor");
}
System.out.println ("hah");
------ For reference only ---------------------- -----------------
if you do not super (3) , they would call the superclass constructor with no arguments , but you have to explicitly call it.
------ For reference only -------------------------------------- -
super (3), shows call the parent class constructor. Became the order of execution , this (1); super (3); A (int i); B (int i);
------ For reference only -------- -------------------------------
abc Reply soy passing abc
------ For reference only ---------------------------------------
abc Reply soy passing abc
------ For reference only --------------------------------------- < br> Well, learn,
------ For reference only ---------------------------------------
to understand this should be clear about the following points:
1. must have a parent class subclass
2. constructor calls between each other
3. If the call to the superclass constructor to perform the specified constructor with no parameters or perform

running order:

public class A {

public A() {
System.out.println("this is a empty constructor");
}

public A(int i) {
System.out.println("this is unempty constructor"); //4
}

public static void main(String[] args) {
B b = new B(); //1
}

}

class B extends A {

public B() { 
this(1); //2
}

public B(int i) { 
super(3); //3
System.out.println("hah"); //5
}

}

------ For reference only ----------------------------------- ----


You still take a closer look java initialization order it, is to initialize the parent class , call the parent class constructor , and then the sub-class , it is recommended you take a look at "java programming ideas "

没有评论:

发表评论