专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 学习攻略 Java学习 Java中的构造函数重载教学

Java中的构造函数重载教学

更新时间:2020-09-17 15:26:38 来源:动力节点 浏览2191次

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

有时候需要用不同的方式初始化一个对象。这可以使用构造函数重载来完成。例如,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中,递归构造函数调用无效。

构造函数重载与方法重载

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

java重载构造方法

以上就是动力节点java培训机构的小编针对“Java中的构造函数重载教学”的内容进行的回答,希望对大家有所帮助,如有疑问,请在线咨询,有专业老师随时为你服务。

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>