In methods and data members, we mentioned that objects in Java are initialized (initialization) when they are created. When initialized, the data members of an object are given an initial value. We can initialize it explicitly. If we do not give the data member an initial value, the data member takes the default initial value according to its type.
Explicit initialization requires that we determine the initial value when writing a program, which is sometimes inconvenient. We can use the constructor (constructor) to initialize the object. Constructors can initialize data members, and can also specify specific actions. These actions are executed automatically when the object is created.
Defining constructors
A constructor is a method. Like the normal method, we define the constructor in the class. The constructor has the following basic characteristics:
1. The name of the constructor is the same as the name of the class
2. Constructor has no return value
We define constructors for the human class:
public class Test
{public
static void Main (string[] args)
{
Human Aperson = new Human (160);
System.out.println (Aperson.getheight ());
}
Class Human
{
/**
* constructor * *
Human (int h)
{
this.height = h;
System.out.println ("I ' m Born");
}
/**
* accessor
/int getheight ()
{return
this.height;
}
int height;
}
The program above will print
Copy Code code as follows:
The constructor can receive a list of parameters like a normal method. Here, the constructor human () receives an integer as an argument. In the body of the method, we assign the integer parameter to the data member height. The constructor does two things when the object is created:
The constructor can receive a list of parameters like a normal method. Here, the constructor human () receives an integer as an argument. In the body of the method, we assign the integer parameter to the data member height. The constructor does two things when the object is created:
1. Provide initial value for data member This.height = h;
2. Perform specific initial operation System.out.println ("I ' m Born");
In this way, we can set the initial value flexibly when we call the constructor, and not be as constrained as the display initialization.
How is the constructor invoked? When we create a class, we use the new Human () method. In fact, we are calling the constructor of the human class. When we do not define the method, Java provides a blank constructor to invoke when new is used. But when we define the constructor, Java invokes the defined constructor when the object is created. At the time of the call, we provided a parameter of 160. As you can see from the final run results, the height of the object is actually initialized to 160.
Priority of the initialization method
In methods and data members, we can see that if we provide an explicit initial value, the data member takes an explicit initial value instead of the default initial value. However, if we provide both an explicit initial value and a constructor to initialize the same data member, the final initial value will be determined by the constructor. For example, the following examples:
public class Test
{public
static void Main (string[] args)
{
Human Aperson = new Human (160);
System.out.println (Aperson.getheight ());
}
Class Human
{
/**
* constructor * *
Human (int h)
{
this.height = h;
}
/**
* accessor
/int getheight ()
{return
this.height;
}
int height=170; Explicit Initialization
}
The results of the operation are:
Copy Code code as follows:
The final initialization value of the object is consistent with the value in the build method. So:
Build Method > Explicit Initial value > default initial value
(In fact, the so-called priorities are related to the order in which they were initialized, and I'll go into that later)
Method overload
You can define more than one constructor in a class, such as:
public class Test
{public
static void Main (string[] args)
{
Human Nezha = new Human ("shit"); C6/>system.out.println (Nezha.getheight ());
}
Class Human
{
/**
* Constructor 1
*
/Human (int h)
{
this.height = h;
System.out.println ("I ' m Born");
}
/**
* Constructor 2
/Human (int h, String s)
{
this.height = h;
System.out.println ("Ne Zha:i ' m born," + s);
}
/**
* accessor
/int getheight ()
{return
this.height;
}
int height;
}
Run Result:
Copy Code code as follows:
Ne Zha:i ' m born, shit
150
Two constructors are defined above, and the names are human. The two constructors have different argument lists.
When you create an object using New, Java determines which constructor to build based on the parameters provided. For example, when building Nezha, we provided two parameters: integer 150 and string "shit", which corresponds to the argument list for the second build method, so Java invokes the second build method.
In Java, Java determines the method to be invoked, based on the method name and the list of parameters, which is called the method overload (methods overloading). Build methods can be overloaded, and common methods can be overloaded, such as the following breath () method:
public class Test
{public
static void Main (string[] args)
{
Human Aperson = new Human ();
Aperson.breath (ten);
}
}
Class Human
{
/**
* Breath () 1 */
void Breath ()
{
System.out.println ("hu...hu ...) ");
}
/**
* Breath () 2 */
void Breath (int rep)
{
int i;
for (i = 0; I < rep i++) {
System.out.println ("Lu...lu ...");
}
int height;
}
Run Result:
Copy Code code as follows:
Lu...lu ...
Lu...lu ...
Lu...lu ...
Lu...lu ...
Lu...lu ...
Lu...lu ...
Lu...lu ...
Lu...lu ...
Lu...lu ...
Lu...lu ...
As you can see, the second breath () method that matches the argument list is invoked because an argument is provided at the time of the call: integer 10.
Summarize
Constructor feature: same name as class, no return value
Constructor Purpose: Initialize, initial operation
Method Overload: Method name + argument list-> which method is actually called