Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand constructors and construction blocks of code

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to understand the constructor and the construction code block". In the daily operation, I believe that many people have doubts about how to understand the constructor and the construction code block. The editor consulted all kinds of data and sorted out the simple and useful operation methods. I hope it will be helpful to answer the doubts about how to understand the constructor and the construction code block. Next, please follow the editor to study!

Foreword:

In order to capture the relationship between constructor blocks and constructors, I summed up a case that deepens your understanding of constructors and constructor blocks.

Before looking at the code, you need to understand one requirement:

Use the Java class to describe a baby, suppose that there are two kinds of babies, one kind of baby has a name at birth, we call this kind of baby Baihu, the other kind of baby has a name after birth, we call this kind of baby unregistered. (in order to make it easier to understand, I call those who already have a name at birth Baihu, and those who are named after birth as household registration is not registered)

/ / Baby public class Baby {int id;// ID card String name;// name / / cry public void cry () {System.out.println (name+ "wow cry.") ;}} class Test {public static void main (String [] args) {/ / create a baby object Baby baby = new Baby (); / / the baby is born baby.id=111; baby.name= "baby"; / / the household registration is not registered with baby.cry ();}}?

Running result:

The little doll is crying.

Because the doll is assigned after birth, so the doll is not registered, we now want to let the baby be born with a name, we need to use a constructor.

The role of the constructor: initialize the corresponding object

Format defined by the constructor:

Modifier function name (formal parameter) {function body. }

Details to pay attention to in the constructor:

1. Constructor does not return a value type

two。 The function name of the constructor must be consistent with the class name

3. The constructor is not called manually by us, but when the corresponding object is created, jvm will actively call the corresponding constructor.

4. If a class does not explicitly write a constructor, the Java compiler adds a no-argument constructor to the class

5. If a class has been shown to write a constructor, the Java compiler will no longer add a no-argument constructor to the class.

6. There can be multiple constructors in a class in the form of function overloading

/ / Baby class public class Baby {int id;// ID String name;// name / / No parameter constructor public Baby () {} / / Parametric constructor public Baby (int id, String name) {this.id = id; this.name = name; System.out.println ("property initialization of Baby is complete!") ;} / / public void cry () {System.out.println (this.name+ "crying") ;} class Test {public static void main (String [] args) {/ / create a baby object Baby baby = new Baby (222nd, "cutie"); / / baby.cry ();}}?

Running result:

The properties of Baby are initialized!

The little cutie is crying.

Through the above household registration and white households, we can have a deep understanding of the constructor. Below, I extend the construction code block, which is still this example, but the demand has changed. Now the demand is that the baby must cry as soon as he is born. If someone understands the above code, they will certainly think of it right away. It is not easy to write the method of crying in the constructor. Yes, this is indeed possible. But there are drawbacks, Java requires concise code, can write less, do not write more, you first look at the following code to know.

/ / Baby class public class Baby {int id;// ID String name;// name / / No parameter constructor public Baby () {cry ();} / Parametric constructor public Baby (int id, String name) {this.id = id; this.name = name; cry () } / / crying public void cry () {System.out.println ("Whoa crying...") ;} class Test {public static void main (String [] args) {/ / create a white household baby object Baby baby = new Baby; / / create a household registration unregistered baby object Baby baby1 = new Baby (); / / household registration is not registered}}?

Running result:

Wow, crying.

Wow, crying.

Although the white household registration is not registered, both babies cry at birth, but there is duplicate code in the two constructors, and the cry () method is repeated twice. In order to solve the problem of duplicated code, let's let the construction code block come out!

The role of the constructor: initialize the corresponding object

The role of constructing code blocks: uniform initialization of objects

Format of the construction code block:

{construct code block}

Note: the curly braces of the construction code block must be in the member position

Category of code block:

1. Construct code block

two。 Local code block

Curly braces are within the method

Function: shorten the life cycle of local variables and save a little bit of memory.

3. Static code block

{static code block}

What to pay attention to when constructing a code block:

When the 1.Java compiler compiles a Java source file, it advances the declaration statement of the member variable to the front of a class.

two。 The initialization of member variables is actually done in the constructor.

3. Once compiled by the Java compiler, the code that constructs the code block is moved to be executed in the constructor, which is executed before the constructor, and the code in the constructor is executed last

4. The explicit initialization of member variables and the code that constructs the code block are executed in the order of the current code

/ / Baby class public class Baby {int id;// ID String name;// name / / Construction code block {cry ();} / No parameter constructor public Baby () {} / / Parametric constructor public Baby (int id, String name) {this.id = id; this.name = name } / / crying public void cry () {System.out.println ("Whoa crying...") ;}} class Test {public static void main (String [] args) {/ / create a white baby object Baby baby = new Baby; / / White household / / create an unregistered baby object Baby baby1 = new Baby (); / / Household registration is not registered / / anonymous object new Baby ();}?

Running result:

Wow, crying.

Wow, crying.

Wow, crying.

/ / Baby class public class Baby {int id;// ID String name;// name / / Construction code block {cry ();} / No parameter constructor public Baby () {} / / Parametric constructor public Baby (int id, String name) {this.id = id; this.name = name } / / crying public void cry () {System.out.println ("Whoa crying...") ;}} class Test {public static void main (String [] args) {/ / partial code block {/ / create a white baby object Baby baby = new Baby; / / White household / / create an unregistered baby object Baby baby1 = new Baby () / / Household registration is not registered / / anonymous object new Baby ();}? At this point, the study on "how to understand constructors and construct blocks of code" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report