Home IT Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Java polymorphic methods inside constructors complete analysis     - The best tools and techniques to find data on a Unix system (Linux)

- Android in the event delivery and handling mechanism (Programming)

- Linux kernel panic (because glibc result) Repair (Linux)

- Ubuntu 14.10 Install Ubuntu Touch Music App 2.0 (Linux)

- Modify grub solve computer startup error: ERROR 17 (Linux)

- Linux top command to get started (Linux)

- Ubuntu disable graphics card (Linux)

- PHP with FastCGI and mod_php Comments (Server)

- MySQL & NoSQL - Memcached widget (Database)

- How to install Ubuntu applications Device 7 (Linux)

- Oracle 12CIN-memory in table spaces (Database)

- Nginx Beginner Guide (Server)

- Linux shell script to adjust the Task Scheduler (Linux)

- Git uses Details (Linux)

- Ubuntu install Wireshark (Linux)

- Introduction and use of the Raspberry Pi (Linux)

- libreadline.so.6: can not open shared object file problem solution (Linux)

- Python several standard types of built-in functions (Programming)

- How a lot of blocking malicious IP address in Linux (Linux)

- Nine tips to protect the security of Linux desktop (Linux)

 
         
  Java polymorphic methods inside constructors complete analysis
     
  Add Date : 2016-11-15      
         
       
         
  Let's look at an example, if you read "Java programming ideas", then there should be the impression.

package com.test.zj;

public class PolyConstructors {

    public static void main (String [] args) {
        // TODO Auto-generated method stub
        new RoundGlyph (5);
    }

}

class RoundGlyph extends Glyph {
    private int radius = 1;

    public RoundGlyph (int r) {
        // TODO Auto-generated constructor stub
        radius = r;
        System.out.println ( "RoundGlyph radius ==" + radius);
    }

    @Override
    void draw () {
        // TODO Auto-generated method stub
        System.out.println ( "RoundGlyph draw () radius ==" + radius);
    }

}

class Glyph {
    void draw () {
        System.out.println ( "print glyph.draw ()");
    }

    Glyph () {
        System.out.println ( "Glyph () before draw ()");
        draw ();
        System.out.println ( "Glyph () after draw ()");

    }

}

For students who are generally based java Here you might think that the output is as follows:

1 Glyph () before draw ()
2 RoundGlyph draw () radius == 1
3 Glyph () after draw ()
4 RoundGlyph radius == 5

But in fact, after you finished running, you will find that his output is this:


Some people may read here still do not understand what I want to express, I write a simple example. Define a parent SuperClass

package com.test.zj;

public class SuperClass
{
    private int superValue;

    public SuperClass ()
    {
        setSuperValue (100);

    }

    public void setSuperValue (int x)
    {
        superValue = x;
    }

}

Then we define a subclass of it:

// The subclass inherits from the parent class superclass
public class SubClass extends SuperClass
{
    private int subValue = 10;

    public SubClass ()
    {

    }
    // This method overrides the parent class
    public void setSuperValue (int x)
    {
        // First call the parent class method
        super.setSuperValue (x);
        // Then we assign a value to its own variables
        subValue = x;

    }

    public void printSubValue ()
    {
        System.out.println ( "subclass subvalue ==" + subValue);
    }

}

Finally, write a main function can be

package com.test.zj;

public class MainClass {

    public static void main (String [] args) {
        // TODO Auto-generated method stub
        SubClass sc = new SubClass ();
        sc.printSubValue ();
    }

}

Well, now I believe many people will think that the results of the second example of the output should be 100

But in fact there is nothing eggs with his actual results are:

Both of these examples that in the end what happened, we look at the direct bytecode Well, this certainly will not be wrong bytecode, jvm bytecode how to write on how to perform.

We take a look at the first example.

It should be obvious to see our main function is to start a new RoundGlyph this object. Then we look at the results this class -c bars

We can see the constructor of the class

The first implementation is this:

That is the first implementation of the assignment constructor glyph then When the constructor glyph completed before execution

Our radius as an int variable before being executed automatically initialized jvm his is 0!

So here you should be able to guess a vague about the constructor glyph executing first, and then give yourself a member variable radius assignment.

Then we look at the glyph have done it:

You see constructor glyph in the middle of the time 13: invokevirtual # 31 here, to execute the draw method, but subclasses we rewrite this draw method

So you see the draw for subclasses to call the constructor in the glyph subclass radius when the assignment has not been executed, so this method subclass

The output value is zero, of course!

When the constructor of the parent class glyph after the execution of our assignments subclass finally be implemented. So here you should be able to understand the first example.

That we can now look to the second example, we are in fact very much the same. We still look at the second example manclass and main functions

Look here first new main function of a subclass of the subclass of it. Then of course we will take a look subclass init method

In fact, this place is a Subclass constructor.

Here you can see very clearly superclass constructor in the constructor subclass of where we are first performed, and then assigned to their subValue 10.

Then we went to see what has been done in the superclass. But in fact we have been able to come here think no matter what you do in the superclass when you finish later

SubValue values are bound to 10.

So when you subclass after object construction is completed at this time his subvalue member variable value is 10, so of course you print out the value of this variable is necessarily 10 a.

Of course, for a little clearer I put superclass constructor little talk about what has been done, although there is what has been done will not affect our conclusions, but still talk about it,

Even if it does not use any eggs. . .

You see this is a moment to call this method setSuperValue it, right, because subclasses override this method so we certainly have to look at the sub-category

This method of doing it:

Can not you see that they call setSupervalue parent class do, then subsequent calls to see you have a iload putfield

These two operations are not that give us a subclass subvalue assignment for you, right. Been here, our object constructor subclass first step:

The constructor calls the parent class even completed, and finally completed after the implementation of his assignment:

Well, these two examples here, even if the analysis is completed.

In fact, the final conclusion is that java programming ideas in to say:

Parent class static member -> subclass static member -> ordinary members of the parent class initialization and initialization block -> parent class constructor -> subclass ordinary member initialization and initialization block -> subclass constructors

If you are interested, you can write a little bit more complex procedures, verify the above conclusion is established, nonsense. . . . This conclusion is certainly valid. but

If you use this command to see them javap -c bytecode deeper, then I believe you can understand it!

Finally, say one more thing, we usually write code, try to avoid such wording above two examples, because this situation caused by the bug difficult to find. . . which is:

Try not to operate subclass member variables in the constructor of the parent class inside. If we want to initialize write very much trouble, consider using the initialization block such a clear way!

Do not ask me why this research, because tmd there is a bug is discovered for a long time for this reason ah! So after you find someone so written, please write direct mail copy to the whole group of complaints.
     
         
       
         
  More:      
 
- Unetbootin make use U disk loading Linux system (Linux)
- Shell Scripting Interview Questions (Programming)
- Linux install and configure Heartbeat (Server)
- SSH service configuration to use a certificate login Linux server (Server)
- The best tools and techniques to find data on a Unix system (Linux)
- Hazelcast integration with MongoDB (Database)
- Linux kernel panic (because glibc result) Repair (Linux)
- Java data structures - the linear form of the single-chain applications (Programming)
- Django1.5 Custom User Model (Programming)
- OpenGL Superb Learning Notes - GLSL language foundation (Programming)
- Several Methods of SSH Auto - login (Linux)
- CentOS 7 x64 compiler installation Tengine 2.0.3 Comments (Server)
- Docker command Detailed (Linux)
- Smooth upgrade to OpenSSH 6.1 Procedure (Linux)
- LinkedList Basic Usage (Programming)
- OpenSUSE installation on CentOS6 (GUI) (Linux)
- Linux CentOS configuration SVN + SVN hook (Server)
- Spring use Cache (Programming)
- How to modify the Emacs Major Mode Shortcuts (Linux)
- CentOS 6.4 installation environment to build Scrapy 0.22 (Linux)
     
           
     
  CopyRight 2002-2016 newfreesoft.com, All Rights Reserved.