java构造函数的重载_Java中的构造函数重载 - Break易站

Java 构造函数

Java中的构造函数重载

除了重载方法外,我们还可以在java中重载构造函数。基于新执行时指定的参数调用重载的构造函数。

我们什么时候需要构造函数重载?

有时候需要用不同的方式初始化一个对象。这可以使用构造函数重载来完成。例如,Thread类有8种类型的构造函数。如果我们不想指定某个线程的任何内容,那么我们可以简单地使用Thread类的默认构造函数,但是如果我们需要指定线程名称,那么我们可以使用String参数来调用Thread类的参数化构造函数,如下所示:

Thread t= new Thread (" MyThread ");

让我们举一个例子来理解构造函数重载的需要。考虑以下只有一个构造函数带三个参数的类Box的实现。

// 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类的上述实现中,我们无法使用这些选项。

这些类型的初始化对象的不同方式的问题可以通过构造函数重载来解决。下面是带构造函数重载的类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 -1.0

Volume of mycube is 343.0

在构造函数重载中使用this()

可以在构造函数重载期间使用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(int num)构造函数。通过在其中使用this()语句,默认的构造函数(Box())将从其中隐式调用,它将使用-1初始化Box的尺寸。

注意:构造函数调用应该是构造函数体中的第一条语句。例如,以下片段无效并引发编译时错误。

Box(int num)

{

boxNo = num;

/ *构造函数调用必须是第一个

语句在构造函数中* /

this(); /*错误*/

}

在构造函数重载时要注意的重点:

调用构造函数必须是Java中构造函数的第一条语句。

如果我们已经定义了任何参数化构造函数,那么编译器不会创建默认构造函数。反之亦然,如果我们没有定义任何构造函数,编译器在编译过程中会默认创建默认构造函数(也称为no-arg构造函数)

在java中,递归构造函数调用无效。

构造函数重载与方法重载

严格来说,构造函数重载与方法重载有点类似。如果我们想要使用不同数量的参数来初始化一个对象,那么当我们需要基于不同参数的方法的不同定义时,我们必须执行构造函数重载,因为我们会重载方法。

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

特大号先生

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值