Java method overload 2

Source: Internet
Author: User

In this example, the box () constructor requires three independent variables, which means that all the defined box objects must pass three parameters to the box () constructor.
For example, the following statement is invalid in the current situation:
Box Ob = new box ();
Box () requires three parameters. Therefore, it is an error to call without parameters.
This will cause some important problems. What if you only want a box and don't care (or know) its original size?
Or, what if you want to use only one value to initialize a cube and the value can be used as all three dimensions of it?
If the box class is written like this, you cannot solve other problems like this, because you can only include three parameters without any other options. Fortunately, the solution to these problems is quite easy: Reload the box constructor so that it can handle the situation just described.
The following Program is an improved version of box. It uses the overload of Box constructor to solve these problems:
/* Here, box defines three constructors to initialize the dimensions of a box various ways .*/
Class box {
Double width;
Double height;
Double depth;
// Constructor used when all dimensions specified
Box (double W, double H, double D ){
Width = W; Height = H; depth = D;
}
// Constructor used when no dimensions specified
Box (){
Width =-1; // use-1 to indicate
Height =-1; // an uninitialized
Depth =-1 ;//
Box
}
// Constructor used when cube is created
Box (double Len ){
Width = height = depth = Len;
}/
// Compute and return volume
Double volume (){
Return width * height * depth;
}
}
Class overloadcons {
Public static void main (string ARGs []) {
// Create boxes using the varous Constructors
Box mybox1 = new box (10, 20, 15 );
Box mybox2 = new box ();
Box mycube = new box (7 );
Double vol;
// Get volume of first box
Vol = mybox1.volume ();
System. Out. println ("volume of mybox1 is" + vol );
// Get volume of second box
Vol = mybox2.volume ();
System. Out. println ("volume of mybox2 is" + vol );
// Get volume of cube
Vol = mycube. Volume ();
System. Out. println ("volume of mycube is" + vol );
}
}
The output of this program is as follows:
Volume of mybox1 is 3000.0 volume of mybox2 is-1.0 volume of mycube is 343.0 in this example, when new is executed, the appropriate constructor is called Based on the specified independent variable.

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.