When we were born, some people had names once they were born, but some people had good names once they were born. So how do we assign values in Java once the object is created?
public class person {String name; // name int age; // age public static void main (string[] args) {person P = new Perso N (); // System.out.println ("Name:" + p.name + "Age:" + p.age); // name = NULL, age = 0; // This child was born with no name and age }
Public classPerson {String name;//name intAge//Age//Construction MethodPerson (String name,intAge ) { This. name = name;//Assigning a name value to an object This. Age = Age;//Assigning an age value to an object } Public Static voidMain (string[] args) {person P=NewPerson ("Zhang San", 1);//A P object of type person is created, and the constructor method is called to assign the object property valueSystem.out.println ("Name:" + p.name + "Age:" + p.age);//name = Zhang San, age = 1; //The child had a name and age when he was first born .}
1. The role of the construction method:
Constructor Method: Initializes the object.
Public classPerson {String name;//name intAge//Age//Full parameter construction methodPerson (String name,intAge ) { This. name = name;//Assigning a name value to an object This. Age = Age;//Assigning an age value to an object } //Non-parametric construction methodPerson () {} Public Static voidMain (string[] args) {person P=NewPerson ("Zhang San", 1); /*depending on the number of arguments created for the object, the JVM goes back to find the appropriate construction method, and all two arguments call a constructor with two parameters. Person (String Name,int age)*/System.out.println ("Name:" + p.name + "Age:" + p.age);//name = Zhang San, age = 1; //when this object was created, it had its own name and age.}
2. The difference between a constructor and a normal function:
(1). A general function is a function that defines what an object should have. The constructor defines some of the things that an object should have when it is built, before it invokes the function. That is, the initialization of the object.
(2). Constructors are called by the JVM when an object is established and initialized to an object. A general function is when an object is established and is executed when the object invokes the feature.
(3). A normal function can be called multiple times with an object, and the constructor is called when the object is created.
(4). The function name of the constructor is the same as the name of the class, and the normal function simply conforms to the naming rules of the identifier.
(5). The constructor does not return a value type.
3. Details of the constructor to note:
(1). When a constructor is not defined in a class, the system assigns a constructor that adds an empty argument to the class. This is the default constructor in the class. When a constructor is customized in a class, the default constructor is not.
Note: Can be verified by the JAVAP command.
(2). Multiple constructors can be defined in a class for different initializations. Multiple constructors exist in a class and are represented in an overloaded form. Because constructors have the same name.
classperosn{Private intID; PrivateString name; Private intAge ; Publicperosn () {Cry (); } PublicPerosn (intID, String name,intAge ) { This. ID =ID; This. Name =name; This. Age =Age ; } Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } Public voidcry () {System.out.println ("Whoa whoa ..."); } }
Question: Every child is required to be born to cry, this code has two constructors, if you need every child born to cry, then you need to call the cry () function in different constructors, but this will cause the code duplication problem, then how to solve it? Constructs a code block.
4. Construct the code block:
Building blocks of code: All objects are uniformly initialized.
classperosn{Private intID; PrivateString name; Private intAge ; {Cry ();//Each Person object is created to execute the code here. } Publicperosn () {Cry (); } PublicPerosn (intID, String name,intAge ) { This. ID =ID; This. Name =name; This. Age =Age ; } Public intgetId () {returnID; } Public voidSetId (intID) { This. ID =ID; } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } Public voidcry () {System.out.println ("Whoa whoa ..."); } }
Specific role:
1: Initialize the object. The object runs as soon as it is established and takes precedence over the constructor.
2: Difference from constructor function
1: Constructs the difference between the code block and the constructor, constructs the code block to give all objects to unify initializes, the constructor functions to the corresponding object initialization.
2: The purpose of building blocks of code: It is to extract the public information from all the construction methods.
For example a child born unified cry
The code is as follows:
classBoy {String name; intAge ; String gender; //constructs a block of code to initialize all objects. {System.out.println ("Cry ... "); } boy () {System.out.println ("Non-parametric construction"); } Boy (String N,intA, String g) {Name=N; Age=A; Gender=G; System.out.println ("With a reference to the structure"); } voidrun () {System.out.println ("Run ..."); }}classDemo9 { Public Static voidMain (string[] args) {System.out.println (); Boy b=NewBoy (); Boy B2=NewBoy ("Jack", 1, "Male"); }}
In-depth understanding of Java constructors