Analysis on Inheriting Internal Class - Java Programming Thoughts;

xiaoxiao2021-03-06  101

About Inheriting Internal Class - Java Programming Thoughts Example Program Analysis; Class Egg2 {Protected Class Yolk {Public Yolk () {System.out.Println ("Egg2.Yolk ()");} public void f () {system.out .println ("egg2.yolk.f ()");}} private yolk y = new yolk (); public egg2 () {system.out.println ("new egg2 ()");} public void insertyolk (Yolk YY) {y = yy;} public void g () {yf ();}}

public class BigEgg2 extends Egg2 {public class Yolk extends Egg2.Yolk {public Yolk () {System.out.println ( "BigEgg2.Yolk ()");} public void f () {System.out.println ( "BigEgg2. Yolk.f () ");}} public bigegg2 () {INSERTYOLK (new yolk ());} public static void main (string [] args) {EGG2 E2 = New BiGGG2 (); E2.G ();} }

Program operation: Egg2.yolk () new egg2 () Egg2.yolk () BiGegg2.yolk () BiGegg2.yolk.f ()

Operation sequence: 1, Egg2 E2 = New BiGGG2 (); Subclass before instantification, first modify the parent class construction method, the parent class construction method first initialize the class member; there is private yolk y = new yolk () ;, y as egg2 The members of the class first initialize; first modify the constructor of Egg2 internal class YOLK; printed Egg2.yolk (); 2, calling EGG2 constructor; printed Egg2.yolk (); 3, calling BiGegg2 constructor; It INSERTYOLK (New Yolk ()); New Yolk here is an instance of the internal class of BiGegg2; this internal class inherits the internal class in EGG2.

So call the constructor of YOLK in EGG2 first; printed Egg2.yolk (); 4, calling BiGegg2.yolk constructor; printed Bigegg2.yolk (); 5, E2.G (); Print BiGegg2. Yolk ();

I don't know if the analysis is right, if not, please advise zhaoqb@neusoft.com or zqb@hanhuasoft.com

转载请注明原文地址:https://www.9cbs.com/read-103186.html

New Post(0)