Construct java Functions

Source: Internet
Author: User
Construct java functions-general Linux technology-Linux programming and kernel information. For more information, see the following section. Every time you create an instance variable, it is boring to initialize all the variables in the class. Even if you add useful functions to methods like setDim (), you have to do the same. If you set an object when it is initially created, the program will be simpler and more concise. Because the initialization requirements are common, Java allows objects to initialize themselves when they are created. This automatic Initialization is completed by using constructors.

Constructor is initialized when an object is created. It has the same name as its class, and its syntax and method are similar. Once the constructor is defined, the constructor is called automatically immediately after the object is created before the new operator is complete. The constructor looks a bit strange because it does not return any value, even a void value. This is because the hidden type in a class constructor is its own class type. The task of the constructor is to initialize the internal state of an object so that the created instance variables can be fully initialized and can be used immediately by the object.

You can override the Box example program so that the Box size can be automatically initialized when the object is created. To achieve this goal, use the constructor instead of setDim. Let's start with a simple constructor that defines to set the size of each box to the same value. Example:

/* Here, Box uses a constructor to initialize

Dimensions of a box.
*/
Class Box {

Double width; double height; double depth;

// This is the constructor for Box.
Box (){
System. out. println ("Constructing Box ");

Width = 10;
Height = 10;
Depth = 10;

}

// Compute and return volume double volume () {return width * height * depth ;}}

Class BoxDemo6 {

Public static void main (String args []) {// declare, allocate, and initialize Box objectsBox mybox1 = new Box (); Box mybox2 = new Box ();

Double vol;

// Get volume of first box
Vol = mybox1.volume ();
System. out. println ("Volume is" + vol );

// Get volume of second box
Vol = mybox2.volume ();
System. out. println ("Volume is" + vol );

}
}

Run the program to generate the following results:

Constructing Box
Constructing Box
Volume is 1, 1000.0
Volume is 1, 1000.0

As you can see, when mybox1 and mybox2 are created, both of them are initialized by the Box constructor. Because the constructor assigns all the boxes of the same size, the length, width, and height are 10, mybox1 and mybox2 will have the same volume. The println () Statement in Box () is only for illustration. Most constructors do not display anything. They initialize only one object.

Before continuing to learn, let's look at the new operator. As you know, when an object is allocated, the following general format is used:

Class-var = new classname ();

Now you can understand why parentheses are required after the class name. Parentheses call the constructor of this class. In this way, in the following line

Box mybox1 = new Box ();

New Box () calls the Box () constructor. If you do not explicitly define a constructor for the class, Java will create a default constructor for the class. This is why the current program did not define constructors in earlier versions of Box.
The default constructor automatically initializes all instance variables to zero. The default constructor is sufficient for simple classes, but it cannot meet the requirements for more complex classes. Once you define your own constructor, the default constructor will no longer be used.

6.5.1 constructor with Independent Variables
Although in the previous example, the Box constructor indeed initializes the Box object, it is not very useful because all the boxes are of the same size. What we need is a way to construct a wide variety of Box objects. An easy solution is to add independent variables to the constructor. As you may have guessed, this will make them more useful. For example, the following Box program defines an independent variable constructor, which sets the size of each specified Box Based on the independent variables. Pay special attention to how the Box object is created.

/* Here, Box uses a parameterized constructor

Initialize the dimensions of a box.
*/
Class Box {

Double width; double height; double depth;

// This is the constructor for Box.

Box (double w, double h, double d) {width = w; height = h; depth = d;

}

// Compute and return volume double volume () {return width * height * depth ;}}

Class BoxDemo7 {

Public static void main (String args []) {// declare, allocate, and initialize Box objectsBox mybox1 = new Box (, 15); Box mybox2 = new Box (, 6, 9 );

Double vol;

// Get volume of first box
Vol = mybox1.volume ();
System. out. println ("Volume is" + vol );

// Get volume of second box
Vol = mybox2.volume ();
System. out. println ("Volume is" + vol );

}
}

The output of this program is as follows:

Volume is 1, 3000.0
Volume is 1, 162.0

As you can see, each object is initialized by the parameters specified by its constructor. For example, in the downstream,

Box mybox1 = new Box (10, 20, 15 );

When new creates an object, the values 10, 20, 15 are passed to the Box () constructor. In this way, the copy width, height, and depth values of mybox1 will respectively include 10, 20, and 15.

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.