当前位置: 动力学知识库 > 问答 > 编程问答 >

Java - Constructors

问题描述:

So, I'm learning Java after learning the basics of Python, and im stucked at Constructors. Here's a small program that im doing to understand it.

package random;

import java.util.Scanner;

public class program {

public static void main(String[] args)

{

Scanner input = new Scanner(System.in);

Cube x = new Cube();

Cube y = new Cube(20,20,20);

System.out.println(x.cubeVolume());

System.out.println(y.cubeVolume());

}

}

And then the Cube Class

package random;

public class Cube {

int lenght,widht,height;

public int cubeVolume()

{

return (lenght*widht*height);

}

Cube()

{

lenght = 10;

widht = 20;

height = 30;

}

Cube(int l, int w, int h){

lenght = l;

widht = w;

height = h;

}

}

But my problem is to undestand why does my y Cube gets the 20,20,20 and does not change to 10,20,30... Why does it chooses Cube(int l, int w, int h) and not the Cube()? What makes the x Cube go to Cube() and y Cube to Cube(int l, int w, int h)?

网友答案:

basically constructor initializes the global variables of the object.

 construct()  ---> non-parametrized constructor

 construct(parameter)  ---> parametrized constructor

example of parametrized constructor:

                                   cube(int l,int w,int h)
                                   {
                                           length=l;
                                           width=w;
                                           height=h;
                                    }

here u had defined two constructors with same class name, which is called constructor overloading(multiple constructors with different parameters)

1st one:

          cube()
          {
             lenght = 10;
             widht = 20;
             height = 30;
           }

2nd one:

            cube(int l, int w, int h)
            {
             lenght = l;
             widht = w;
             height = h;
             }

So here u had defined two constructors to initializes the global variables of object. Now, as hope you know that only one constructor works at a time to initialize the object and you have to call that constructor during object creation.

here you had created two objects:

1st object: Cube x = new Cube(no argument passed); //no argument passed

In above line constructor cube had been called which initializes the global variable length, widht, height with values 10,20,30. Now, here we did not pass any argument to the constructor so constructor with no parameter had called, which is cube()

2nd object: Cube y = new Cube(20,20,20);

here constructor cube(int l,int w,int h) had been called because here we had defined three parameter -->int l, int w,int h and while calling the constructor we passed three argument -->20,20,20 so parameter matched with argument and the parametrized constructor had called.

so here two object exists, object x has values length=10,widht=20,heigth=30. And other object y with values length=20,width=20,heigth=20;

网友答案:

It will look for the constructor that matches the constructor you are calling.
x cube is constructed with zero arguments, and so the no-arg constructor will be selected.
y cube is constructed with three arguments, and so the three-arg constructor will be selected.

网友答案:
  1. It would help to show the output from your program, which helps in debugging.

  2. width, length and length are spelled using : 'th', not 'ht'. This will not affect how your program compiles or runs, but will help others read your code.

  3. In direct answer to your question, you have two overloaded constructors:

    Cube(); Cube(int length, int width, int height);

    The constructor will be picked by the compiler based on the arguments that you supply. Only one constructor is used (and if you do not supply a constructor, a default Cube() {} is created by the compiler, along with a default super() constructor, and so on).

分享给朋友:
您可能感兴趣的文章:
随机阅读: