Java basics 03 constructor methods and method Overloading

Source: Internet
Author: User

In methods and data members, we mentioned that objects in Java will be initialized during creation ). During initialization, the data member of the object is assigned an initial value. We can determine an initial value when declaring a data member, which is called Explicit initialization. If we do not assign an initial value to a data member, the data member uses the default initial value based on its type.

In addition to the above two initialization methods, we can also use constructor to initialize the object. In addition to initializing data members, the constructor can also specify specific operations. These operations are automatically executed when an object is created.

 

Define Constructor
Like normal methods, we define constructor methods in the class. The constructor has the following basic features:

1. the constructor name is the same as the class name.
2. No return value for the constructor
 

We define the construction method of 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 above program will print

I'm born
160

 

Let's first look at the definition of the constructor. Constructor can receive parameter lists like normal methods. Here, the constructor Human () receives an integer as a parameter. In the body of the method, we assign the integer parameter to the height of the data member. Since the constructor does not return values, the constructor does not need to specify the type of returned values. The main function of constructor is:

• Provide the data member with an initial value of this. height = h;
• Execute a specific initial operation System. out. println ("I'm born ");
 

Let's look at the call of the constructor. We used the new Human () method when creating classes. In fact, we are calling the Human class constructor. If this method is not defined, Java provides a blank constructor to call it when new is used. However, when we define a constructor, Java calls the defined constructor when creating an object. During the call, we provide a parameter of 160. The final running result also shows that the object height is indeed initialized to 160.

 
Initial method priority
In methods and data members, we can see that if we provide explicit initial values, the data members will adopt explicit initial values instead of default initial values. However, if we provide both an explicit initial value and initialize the same data member in the constructor, the final initial value will be determined by the constructor. For example:


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 running result is:

160

The final initialization value of the object is the same as the value in the build method. Therefore:

Build method> explicit initial value> default initial value

(In fact, the so-called priority is related to the execution sequence during initialization. I will go into this point later)

 

Method overload
More than one constructor can be defined in a class, for example:


Public class Test
{
Public static void main (String [] args)
{
Human neZha = new Human (150, "shit ");
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;
} Running result:

Ne Zha: I'm born, shit
150

 

The preceding two constructor methods are defined, and the names are all Human. The two constructor methods have different parameter lists.

When using new to create an object, Java determines which constructor to build based on the provided parameters. For example, when constructing neZha, we provide two parameters: integer 150 and string "shit", which correspond to the parameter list of the second build method, so Java will call the second build method.

In Java, Java determines the method to be called based on the method name and parameter list. This is called method overloading ). The build method can be overloaded, or a common method can be overloaded, for example, the following breath () method:


Public class Test
{
Public static void main (String [] args)
{
Human aPerson = new Human ();
APerson. breath (10 );
}

}

Class Human
{
/**
* Breath () 1
*/
Void breath ()
{
System. out. println ("hu ...");
}


/**
* Breath () 2
*/
Void breath (int rep)
{
Int I;
For (I = 0; I <rep; I ++ ){
System. out. println ("lu ...");
}
}

Int height;
} Running result:

Lu...
Lu...
Lu...
Lu...
Lu...
Lu...
Lu...
Lu...
Lu...
Lu...

 

We can see that the Second breath () method that matches the parameter list is called because an integer 10 is provided during the call.

 

Related Article

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.