Constructor

xiaoxiao2021-03-06  14

Review

First review the knowledge about the constructor

A brief overview of the implementation steps of the constructor:

(1) Binding binding parameters

(2) THIS ()

(3) Super () -> until Object is terminated

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Here, the upstream process is completed. Since this process is completely automated, we can ignore

But be sure to remember that all member variables from this will initialize the default value.

Then the Object is from top, down, down derivation

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

(4) Execution domain variable assignment statement

(5) Perform the statement in the constructor

We are familiar with this process.

If you only think about the process of the uplink as the process of initializing all member variables, then we can ignore it

(But don't forget the existence of the super () process, which is also one of the test points), and puts energy into the process of being lower.

After all, our code is executed in this process.

Therefore, we can regard the initial process of the constructor as an executive to the next level from the top of the Object.

Process of MARCUS Green, Reference

http://www.jchq.net)

Now let's see some excellent questions:

2. Example discussion

(1) Q22 from Hardest Mock EXAM

What Will Be Written to The Standard Output

WHEN THE FOLLOWING PROGRAM Is Run?

Class base {

INT I;

Base () {

Add (1);

}

Void Add (int V) {

i = v;

}

Void print () {

System.out.println (i);

}

}

Class Extension Extends Base {

EXTENSION () {

Add (2);

}

Void Add (int V) {

i = v * 2;

}

}

Public class qd073 {

Public static void main (string args []) {

BOGO (New Extension ());

}

Static void Bogo (Base B) {

B.Add (8);

B.Print ();

}

}

1) 9

2) 18

3) 20

4) 21

5) 22

This topic not only examines the construction process of the constructor, but also inspects knowledge about polymorphism.

BOGO (New Extention ()); here New has an extention object

Obviously call the constructor and initialize an object, BOGO () is bound to the BASE class incoming BOGO method

So saying that this statement like the following: base v = new extention (); BOGO (V);

Therefore, the process of extention () initialization is:

Upstream - initialize all instance variables, at this time, i = 0

Dry derived - the top execution constructor, (Object constructor is empty, so continued)

First execute the parent class base constructor add (1), note that due to the polymorphism and dynamic binding

Add (1) Call the virtual call () method of the EXTENTION class, not the add () method of the Base class

Therefore, after add (1), i = 2;

Continue to execute subclass constructors Add (2), still call the add () method of the EXTENTION class,

After execution, i = 6 constructors are executed

After running B.Add (8), i = 22

The answer is 5

(2)

What is the output of the folowing program

Public class test {

Private I = givemej ();

Private int J = 10;

Private int givemej () {

Return J;

}

Public static void main (string args []) {

System.out.println ((New Test ()). I);

}

}

SELECT One Correct Answer

a. Compiler Error Complaining About Access Restriction of Private Variables

OF Aquestion.

b. Compiler Error Complaining About Forward Referencing.

C. NO Compilation Error - The Output IS 0;

D. no compilation error - The Output IS 10;

e. no compilation error, but runtime error;

This topic is slightly simple, but the domain variable is easily confused by the method.

If you don't firmly determine your belief, you will be confused.

The analysis steps are the same as the previous example:

Upstream - Initialization All Member Variable I = 0, J = 0

Substance - the fourth step of the implementation process of the constructor is used herein, that is, performing explicit-domain variables.

i calls the givemej () method to assign yourself, at this time j = 0, therefore, i = 0

Then J assigns the initial value of 10; then, execute the last step of the New process, perform the constructor

Since the constructor is the default constructor, there is no code execution, the New process ends.

So the answer is C

(3)

In the case of the above example, the I assignment statement is modified, and it is changed to:

Private int i = new test (). givemej ();

In the same step analysis, you will find that when the fourth step in the construction process is the value of I specified

Another new Test object, so you need to enter a new New New process

The new process has entered a new New New New process in the same steps ............

It seems that there is no end? That's right, this program repeats New itself in the stack.

Will lead to stack overflow

So the answer is e

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

New Post(0)