Java object instance in-depth understanding of the formation examples
Abstract: In-depth understanding of Java objects examples generated example
</ Td> </ tr> <tr> <td height="35" valign="top" class="ArticleTeitle"> <table width = "100%" border = "0" cellspacing = "0" cellpadding = " 0 "> <tr> <td width="442" height="86" valign="top">
Class A (public int Avar; public A () (System.out.println ( "AAA"); doSomething ();) public void doSomething () (Avar = 1111; System.out.println ( "A.doSomething () ");)) public class B extends A (public int Bvar = 2222; public B () (System.out.println (" BBB "); doSomething (); System.out.println (" Avar = "+ Avar)
public void doSomething () (System.out.println ( "Bvar =" + Bvar);) public static void main (String [] args) (new B ();))
Explain:
This is in order, first of all, have to generate B-A (not the father, there would be no sub-)
Therefore, the constructor called A, the output AAA,
Then call methods dosomething,! ! !
Note: A B of the methodology is covered
And you generation is the target B,
Therefore, it calls the method B,
BVAR because there is no given value,
So automatically initialized to 0,
Then generate B object,
BVAR first initialize variables,
Then call the constructor output BBB,
Then call methods
Then BVAR has initialization,
Therefore, the output BVAR = 2222,
The object A in the absence of variable AVAR A method called targeted dosomething,
Therefore, its value is 0, then the output 0
Full output on the following:
</ Td> <td width="242" valign="top">
</ Td> </ tr> <tr> <td height="20" colspan="2">
</ Td> </ tr> </ table>
AAA
Bvar = 0
BBB
Bvar = 2222
Avar = 0
NOTE: initialization sequence, when the succession, a supra-class, object, object generation, as Mr. static variables, and then the general variables, and then call the constructor! So when the ultra-generation object type, object Health costs, the same order! When the method is covered, the current object method call! This was attention.
Below this subject you to be elected to the answer? :
What will happen when you attempt to compile and run the following code? Class Base (int i = 99; public void amethod () (System.out.println ( "Base.amethod ()");) Base () (amethod ( );)) public class Derived extends Base (int i = 1; public static void main (String argv []) (Base b = new Derived (); System.out.println (bi); b.amethod (); ) public void amethod () (System.out.println ( "Derived.amethod ()");)) A. Derived. amethod () -1 Derived.amethod () B. Derived. amethod () 99 Derived.amethod ( ) C. 99 Derived.amethod () D. Compile time error
Function TempSave (ElementID) (CommentsPersistDiv.setAttribute ( "CommentContent" document.getElementById (ElementID). Value); CommentsPersistDiv.save ( "CommentXMLStore");) function Restore (ElementID) (CommentsPersistDiv.load ( "CommentXMLStore"); document . getElementById (ElementID). CommentsPersistDiv.getAttribute value = ( "CommentContent");) </ td> </ tr> <tr>
↑ Back
Tags: examples, java object, Object
Releated Java Articles
Comments
Leave a Reply
Java object instance in-depth understanding of the formation examples!
Code as follows:
Class A (
Public int Avar;
Public A () (
System.out.println ( "AAA");
DoSomething ();
)
Public void doSomething () (
Avar = 1111;
System.out.println ( "A.doSomething ()");
)
)
Public class B extends A (
Public int Bvar = 2222;
Public B () (
System.out.println ( "BBB");
DoSomething ();
System.out.println ( "Avar =" + Avar);
)
Public void doSomething () (
System.out.println ( "Bvar =" + Bvar);
)
Public static void main (String [] args) (
New B ();
)
)
Explain:
This is in order, first of all, have to generate B-A,
Therefore, the constructor called A, the output AAA,
Then call methods dosomething,! ! !
Note: A B of the methodology is covered
And you generation is the target B,
Therefore, it calls the method B,
BVAR because there is no given value,
So automatically initialized to 0,
Then generate B object,
BVAR first initialize variables,
Then call the constructor output BBB,
Then call methods
Then BVAR has initialization,
Therefore, the output BVAR = 2222,
The object A in the absence of variable AVAR A method called targeted dosomething,
Therefore, its value is 0, then the output 0
Full output on the following:
AAA
Bvar = 0
BBB
Bvar = 2222
Avar = 0
NOTE: initialization sequence, when the succession, a supra-class, object, object generation, as Mr. static variables, and then the general variables, and then call the constructor! So when the ultra-generation object type, object Health costs, the same order! When the method is covered, the current object method call! This attention to a
Tags: examples, java object, Object






