{Java} an interesting situation about class attribute initialization. java Initialization

Source: Internet
Author: User

{Java} an interesting situation about class attribute initialization. java Initialization

========================================================== ======================
Author: qiujuer
Blog: blog.csdn.net/qiujuer
Website: www.qiujuer.net
Open-source Library: github.com/qiujuer/Genius-Android
Open-source Library: github.com/qiujuer/Blink
Reprinted please indicate the source: http://blog.csdn.net/qiujuer/article/details/46747975
-- Learning Open Source for open source; a beginner's mentality is shared with you!

========================================================== ======================

Interesting?

What is fun? It's actually funny.
I haven't written a blog for a while. I recently got an APP. I will post it later. You are welcome to test it.

Problem

This situation is somewhat special. At that time, I was working on the APP's setting module. The result showed that no matter how the setting saves the second value, it was an initial value, after tracking, we found that it was saved to a file, which is amazing. After thinking about the problem, I certainly tracked the breakpoint of the obtained value while obtaining the value, and found that the obtained value was previously saved, after the class constructor, the value becomes the initial value. Why?

Best of mind, Sister ~~~

I like to look at the Code directly.

Code
package net.qiujuer;public abstract class People {    public People() {        initValue();    }    public abstract void initValue();    public void show() {        System.out.printf("Show:" + toString());    }}

A parent class that calls the initialization method in the constructor.

Package net. qiujuer; public class Man extends People {private String mName = "Xiaoqian"; private int mAge = 18; @ Override public void initValue () {mName = "Qiujuer "; mAge = 23 ;}@ Override public String toString () {return "Name:" + (mName = null? "NULL": mName) + "Age:" + mAge + "\ n ";}}

Implementation class to implement its specific initialization method, and perform some attribute operations in this method.

package net.qiujuer;public class MainClass {    public static void main(String[] args) {        Man man = new Man();        man.show();    }}

Test class, in which new is directly added and the method is called.
What is the result?

Who can guess?

The output is:

Show: Name: Xiao Qian Age: 18

How is it? Did you think of it? Why? Try it yourself.

Change

Let's output some details.

First, change the constructor of the parent class:

    public People() {        System.out.printf("People-1:" + toString());        initValue();        System.out.printf("People-2:" + toString());    }

Then change Man's constructor:

    public Man() {        super();        System.out.printf("Man:" + toString());    }

Run it again ~~

What is the result?

Think about it first, and then look at it to estimate its usefulness.

Forget it. Go directly ~

Result output:

People-1: Name: NULL Age: 0
People-2: Name: Qiujuer Age: 23
Man: Name: min Xiaoqian Age: 18
Show: Name: Xiao Qian Age: 18

Did you think of it?

Conclusion

I think of these:
-During class attribute initialization, the default value of direct value assignment will be performed after the constructor of the parent class.
-When the parent class is operated, its subclass attribute does not have a value: the mName attribute is NULL.
-In the parent class, you can assign values to the attributes of the implemented class by using the method. However, when a value is assigned to the implemented class by default, a secondary value is automatically assigned.

An unintentional move may cause a huge BUG ~

Other

About the APP, this APP is used to record some software that is important to you on the day, and uses the time sequence method to give you a silent reminder.
{UPMiss} misses your summer
:

  • Baidu
  • The new version of pods is still under review!
  • Meizu

========================================================== ======================
Author: qiujuer
Blog: blog.csdn.net/qiujuer
Website: www.qiujuer.net
Open-source Library: github.com/qiujuer/Genius-Android
Open-source Library: github.com/qiujuer/Blink
Reprinted please indicate the source: http://blog.csdn.net/qiujuer/article/details/46747975
-- Learning Open Source for open source; a beginner's mentality is shared with you!

========================================================== ======================

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.