Several questions in Java inheritance

Source: Internet
Author: User

First question:

1 /*2 See program Write results:3 A: The principle of accessing member variables: the nearest principle. 4 questions about B:this and super:5 this accesses members of this class6 Super access to the members of the parent class (as you can understand)7 C: The default constructor method for the parent class is executed before all the constructor methods of the subclass are executed. 8 D: Initialization process for a class:9 the initialization process for member variables is as follows:Ten Default Initialization One Display Initialization A constructor Method Initialization -                  - The result of the output is: the Fu - Zi -  - -  - + Ten - */ + classFu { A      Public intnum = 10; at      PublicFu () { -System.out.println ("Fu"); -     } - } -  - classZiextendsFu { in      Public intnum = 20; -      PublicZi () { toSystem.out.println ("Zi"); +     } -      Public voidShow () { the         intnum = 30; *SYSTEM.OUT.PRINTLN (num);// - $System.out.println ( This. num);// -Panax NotoginsengSystem.out.println (Super. num);//Ten -     } the } + classExtendstest { A      Public Static voidMain (string[] args) { theZi z =NewZi (); + z.show (); -     } $}

The second question:

1 /*2 See program Write results:3 A: A static block of code for a class that constructs a block of code to construct the execution flow of a method:4 static code blocks > Constructing code blocks > Construction Methods5 B: Static content is loaded with the load of the class,6 that is, the contents of the static code block will be executed first. 7 C: Building blocks of code8 appearing outside of a method in a class (that is, in a member position in a class), you can put together the same code in multiple construction method methods to initialize the object .9 each call to the construction method executes and is executed before the method is constructed. Ten C: All constructor methods of a subclass will access the parent class's parameterless constructor by default.  One          A the output is: - Static code block FU - static code block Zi the construct code block FU - Construction Method Fu - Constructing code blocks zi - Construction Method Zi + */ - classFu { +     Static { ASystem.out.println ("Static code block FU"); at     } -  -     { -System.out.println ("Construct code block FU"); -     } -  in      PublicFu () { -System.out.println ("Construction Method Fu"); to     } + } -  the classZiextendsFu { *     Static { $System.out.println ("Static code block Zi");Panax Notoginseng     } -  the     { +System.out.println ("Construct code block Zi"); A     } the  +      PublicZi () { -System.out.println ("Construction Method Zi"); $     } $ } -  - classExtendsTest2 { the      Public Static voidMain (string[] args) { -Zi z =NewZi ();Wuyi     } the}

Question three:

1 /*2 See program Write results:3 A: Problem with member variables4 int x = 10;//member variable x is the base type5 Student s = new Student ();//member variable S is a reference type6 B: Initialization of a class7 Initialize the member variables first:8 Default Initialization9 Display InitializationTen constructor Method Initialization One C: Initialization of the child parent class (hierarchical initialization) A initializes the parent class first, and then initializes the child class.  -              - Results: the yxyz -          - Questions: - Although the constructor method in the subclass has a super () by default; + But the initialization is not done in that order.  - instead, it follows a hierarchical initialization.  + super (); it simply means initializing the parent data before initializing the subclass data.  A */ at  - classX { -     //member variable (reference type) -Y B =NewY (); -     //Non-parametric construction method - X () { inSystem.out.print ("X"); -     } to } +  - classY { the     //Non-parametric construction method * Y () { $System.out.print ("Y");Panax Notoginseng     } - } the  +  Public classZextendsX { A     //member variable (reference type) theY y =NewY (); +     //Non-parametric construction method - Z () { $         //super ();//it simply means initializing the parent class data before initializing the subclass data.  $System.out.print ("Z"); -     } -      Public Static voidMain (string[] args) { the         NewZ (); -     }Wuyi}

Several questions in Java inheritance

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.