除了重载方法,我们还可以重载java中的构造函数。重载构造函数是根据 新 被处决了。
什么时候需要构造函数重载?
有时需要以不同的方式初始化对象。这可以使用构造函数重载来完成。例如,Thread类有8种类型的构造函数。如果我们不想指定关于线程的任何内容,那么我们可以简单地使用thread类的默认构造函数,但是如果我们需要指定线程名称,那么我们可以使用如下字符串参数调用thread类的参数化构造函数:
Thread t= new Thread (" MyThread ");
让我们举一个例子来理解构造函数重载的需求。考虑以下类的实现,其中只有一个构造函数接受三个参数。
// An example class to understand need of // constructor overloading. class Box { double width, height,depth; // constructor used when all dimensions // specified Box(double w, double h, double d) { width = w; height = h; depth = d; } // compute and return volume double volume() { return width * height * depth; } }
正如我们所见,Box()构造函数需要三个参数。这意味着Box对象的所有声明都必须向Box()构造函数传递三个参数。例如,以下语句当前无效:
Box ob = new Box();
由于Box()需要三个参数,因此在没有它们的情况下调用它是错误的。假设我们只是想要一个没有初始维度的长方体对象,或者想要通过只指定一个用于所有三个维度的值来初始化一个立方体。从上述Box类的实现来看,我们无法使用这些选项。
这些类型的初始化对象的不同方法的问题可以通过构造函数重载来解决。下面是带有构造函数重载的类框的改进版本。
// Java program to illustrate // Constructor Overloading class Box { double width, height, 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 = height = depth = 0 ; } // constructor used when cube is created Box( double len) { width = height = depth = len; } // compute and return volume double volume() { return width * height * depth; } } // Driver code public class Test { public static void main(String args[]) { // create boxes using the various // 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); } } |
输出:
Volume of mybox1 is 3000.0 Volume of mybox2 is 0.0 Volume of mycube is 343.0
在构造函数重载中使用this()
这个()引用 可以在构造函数重载期间使用,从参数化构造函数隐式调用默认构造函数。请注意,this()应该是构造函数中的第一条语句。
// Java program to illustrate role of this() in // Constructor Overloading class Box { double width, height, depth; int boxNo; // constructor used when all dimensions and // boxNo specified Box( double w, double h, double d, int num) { width = w; height = h; depth = d; boxNo = num; } // constructor used when no dimensions specified Box() { // an empty box width = height = depth = 0 ; } // constructor used when only boxNo specified Box( int num) { // this() is used for calling the default // constructor from parameterized constructor this (); boxNo = num; } public static void main(String[] args) { // create box using only boxNo Box box1 = new Box( 1 ); // getting initial width of box1 System.out.println(box1.width); } } |
输出:
0.0
正如我们在上面的程序中所看到的,我们在创建对象时只使用Box number调用了Box(int num)构造函数。通过在其中使用this()语句,默认构造函数(Box())将从中隐式调用,它将用0初始化Box的维度。
注: 构造函数调用应该是构造函数体中的第一条语句。例如,以下片段无效,并引发编译时错误。
Box(int num) { boxNo = num; /* Constructor call must be the first statement in a constructor */ this(); /*ERROR*/ }
进行构造函数重载时需要注意的要点:
- 构造函数调用必须是 第一 Java中的构造函数语句。
- 若我们已经定义了任何参数化构造函数,那个么编译器将不会创建默认构造函数。反之亦然,如果我们不定义任何构造函数,编译器会在编译期间默认创建默认构造函数(也称为无参数构造函数)
- 递归构造函数调用在java中无效。
构造函数重载与方法重载
严格来说,构造函数重载与方法重载有些相似。如果我们想使用不同数量的参数来初始化一个对象,那么我们必须使用构造函数重载,就像我们在需要基于不同参数的方法的不同定义时使用方法重载一样。
本文由 高拉夫·米格拉尼 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。