Javase a few simple questions about the easiest error

Source: Internet
Author: User

Case 1.

Package Cn.itcast.oop;public class Thisdemo {public static void main (string[] args) {Student s=new Student (); S.setname ("Lee Formulation "); S.setage (22); String name=s.getname (); int age=s.getage (); SYSTEM.OUT.PRINTLN ("Student's name is:" +name+ "  Student's age is:" +age);}} Class Student{private string name;private int age;public string getName () {return name;//this is omitted by default, actually this.name} public void SetName (String name) {name = name;} public int getage () {return age;} public void Setage (int.) {age = Age;}}

This piece of code is very simple, but very easy error! The key point of the error is this.

Execution Result:

Why is that? Why the printed result is not--the student's name is: Li Weikang The age of the student is: 22



Case 2.

Package Cn.itcast.oop;public class Studentdemo {public static void main (string[] args) {person s=new person ();}} Class Person{private String name= "Brigitte";p rivate int age=27;public person () {name= "Liu Yi"; age=30;}}
Person s=new person ();

A: Load the Student.class file into memory

B: Open a space in the stack memory for s variables

C: Request a space for human objects in heap memory

D: The default initialization is made to the member. NULL 0

E: Displays initialization of the member variable. Brigitte, 27

F: Initialize the member variable by constructing the method Liu Yi. 30

G: Data initialization is complete, then the address value of the heap memory is assigned to the s variable of the stack memory
3. Overview of Inheritance

A:java only supports single inheritance of classes does not support multiple inheritance of classes

B: subclasses can only inherit all non-private members of the parent class (member methods and member variables)
C: Subclasses cannot inherit the constructor of the parent class, but are able to access the parent class construction method through super (immediately speaking) keyword.

D: the member variable in the subclass is the same as the member variable name in the parent class.
To access a variable's lookup order in a subclass method:

A: Find the local scope of the subclass method, and use it

B: In the subclass of the member scope to find, there is the use of

C: In the member scope of the parent class, you can use the

D: Suppose you can't find it yet. On the error.

E:This represents the appropriate reference for this class. Super represents the identity of the parent storage space (which can be understood as a parent class reference, capable of manipulating members of the parent class)

F: Relationship of construction methods in inheritance
All of the constructor methods in the subclass will access the constructor of the parent class by default: Because the subclass inherits the data from the parent class, it may also use the parent class's data. Therefore, the initialization of the parent data must be completed before the subclass initializes.

Note: The first statement of each constructed method of a subclass is: Super ();

G: Suppose the parent class does not have a constructor method, what happens to the constructor of the subclass? Error.
How to solve it?

1): Add a non-participating constructor to the parent class

2): Call the parent class by using Superkeyword to display the Join constructor method

3 ): Subclasses use this to call other constructors of this class, and a subclass must have a way to access the constructor of the parent class, otherwise the parent data is not initialized.

Precautions:
This (...) or Super (...) Must now be on the first statement.
Assuming that it is not placed on the first statement, it is possible to initialize the data of the parent class more than once, so it must be placed on the first statement.

H: Relationship of member methods in Inheritance: 1): The methods in the subclass are not the same as the method declarations in the parent class, this is too simple. 2): The method in the subclass is the same as the method declaration in the parent class, how does this play?


To invoke a method from a subclass object:
A: Find the subclass first. See if there's any way to use
B: Look again at the parent class, there is no such method. Use it if you have one

C: If there is no error.

I: Considerations for Method overrides
1): Private methods in the parent class cannot be overridden-because the parent class private method subclasses simply cannot inherit
2): When subclasses override parent class methods. Access permissions cannot be lower--and best on the same
3): Parent class static method, subclass must also be overridden by static method
In fact, this is not a method of rewriting. But the phenomenon is really so, as to why not the method rewrite, polymorphic I will say

When a subclass overrides a parent class method, it is best to declare exactly the same.

Examples:

Class Fu {static {System.out.println ("Static code block Fu");} {System.out.println ("Construct code block Fu");} Public Fu () {System.out.println ("construction Method Fu");}} Class Zi extends Fu {static {System.out.println ("Static code block Zi");} {System.out.println ("Construct code block Zi");} Public Zi () {System.out.println ("constructor method Zi");}} Class ExtendsTest2 {public static void main (string[] args) {Zi z = new Zi (); Zi z2 = new Zi ();}}
Operation Result:

See Program Write results:
A: a static block of code for a class that constructs a code block and constructs a method of running a process
Static code blocks > Constructing code blocks > Construction Methods
B: Static content is loaded as the class is loaded into
The contents of the static code block will run first
C: The initialization of the parent class before the subclass is initialized
The result is:
Static code block FU
Static code block Zi
Construct code block FU
Construction Method Fu
Constructing code blocks Zi
Construction Method Zi

Classic Interview Questions:

Class X {x () {System.out.print ("X");} Y B = new Y ();} Class Y {y () {System.out.print ("Y");}} public class Z extends X {z () {//supersystem.out.print ("z");} Y y = new Y ();p ublic static void Main (string[] args) {new Z ();}}
Remember a word: Initialize the parent data first, and then initialize the subclass data

Therefore, the running process is: according to the order of initialization: default initialization, display initialization, construct initialization

1. First initialize the parent class X: Run member variable to display initialization y y=new y (), so call Y's constructor to print Y

2. Constructor initialization of the parent class, run code in the construction method to print X

3. Subclass Initialize class Z: Run member variable initialize y y=new y () so call Y's construction method to print Y

4. Constructor initialization for subclasses: Run code in construction method print Z

The result is: yxyz
Debug mode:



Case:

Package Cn.itcast.extend;public class Demo {public static void main (string[] args) {Zi z=new Zi (); Z.show ();}} Class Fu{int num=10;public void Show () {System.out.println (num);}} Class Zi extends Fu{int num=20;public void Show () {System.out.println (num);}}
The result is no doubt:

Remove the Show method for subclasses

Package Cn.itcast.extend;public class Demo {public static void main (string[] args) {Zi z=new Zi (); Z.show ();}} Class Fu{int num=10;public void Show () {System.out.println (num);}} Class Zi extends Fu{int num=20;}

The answer is: 10

Why: Look in the parent class because the method show () found in the subclass does not find it.


Javase a few simple questions about the easiest error

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.