String STR="aa".
The Person () {
show();
}
Public void show () {
System.out.println(str);
}
}
The class Student extends the Person {
Private String STR="bb".
@ Override
Public void show () {
System.out.println(str);
}
}
The class Test3 {
Public static void main (String [] args) {
Person p=new Student ();
}
}
Why the print null, the structure of the parent class call the show method is what is a subclass of the show
CodePudding user response:
You have new a subclass object, use the parent class to pick up.CodePudding user response:
Because the creation is a subclass Student object, this point to Student object, so call subclasses Student showPrint null because of class loading sequence, the superclass constructor calls the subclass methods, subclass Student member variable has not been initialized, so is null
CodePudding user response:
Because it is a new Student (), so called the subclass Student. The show (),Order is instantiated object, instantiate the superclass member variable, the parent class structure, subclasses member variable, subclass structure,
Therefore:
Procedure to initialize the parent class: first instantiation members of STR="aa"; Then perform its structure, for instance is a subclass of call subclasses show method, this time a subclass STR=null, so the output empty
Program and initial anti-fuzzy class: instance, anti-fuzzy class member STR="bb"; Then execute subclass structure, because there is no structure so subclasses will automatically perform a no-parameter constructor, empty
You can add to the same and give the student a construct, anti-fuzzy initial class, because the bb initialization, multi-output bb information
CodePudding user response: