Java 创建单个对象时如何执行多个构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18999635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:14:24  来源:igfitidea点击:

How to execute multiple constructor, when creating single object

javaconstructor

提问by CodeCrypt

I want to execute multiple constructor, while creating a single object. For example, I have a class definition like this-

我想在创建单个对象的同时执行多个构造函数。例如,我有一个这样的类定义-

public class Prg
{
    public Prg()
    {
        System.out.println("In default constructor");
    }
    public Prg(int a)
    {
        System.out.println("In single parameter constructor");
    }
    public Prg(int b, int c)
    {
        System.out.println("In multiple parameter constructor");
    }
}

And I am trying to achieve it by the following code -

我正在尝试通过以下代码来实现它 -

public class Prg
{
    public Prg()
    {
        System.out.println("In default constructor");
    }
    public Prg(int a)
    {
        Prg();
        System.out.println("In single parameter constructor");
    }
    public Prg(int b, int c)
    {
        Prg(b);
        System.out.println("In multiple parameter constructor");
    }
    public static void main(String s[])
    {
        Prg obj = new Prg(10, 20);
    }
}

But in this case it is generating errors like -

但在这种情况下,它会产生如下错误 -

Prg.java:11: error: cannot find symbol
            Prg();
            ^
  symbol:   method Prg()
  location: class Prg
Prg.java:16: error: cannot find symbol
            Prg(b);
            ^
  symbol:   method Prg(int)
  location: class Prg
2 errors

Thanks

谢谢

采纳答案by Manuel Manhart

Use this()instead of Prg()in your constructors

在构造函数中使用this()而不是Prg()

回答by ahmedalkaff

When calling other constructors Use this()instead of Prg()

调用其他构造函数时使用this()代替Prg()

回答by Jayamohan

You should use thisstatement.

你应该使用this语句。

e.g.

例如

public Prg(int b, int c)
{
    this(b);
    System.out.println("In multiple parameter constructor");
}

回答by Prasad Kharkar

Use thisinstead of Prg

使用this代替Prg

    public Prg()
    {
        System.out.println("In default constructor");
    }
    public Prg(int a)
    {
        this();
        System.out.println("In single parameter constructor");
    }
    public Prg(int b, int c)
    {
        this(b);
        System.out.println("In multiple parameter constructor");
    }

回答by SpringLearner

use thiskeyword.Full running code is as follows

使用this关键字。完整运行代码如下

public class Prg
{
    public Prg()
    {
        System.out.println("In default constructor");
    }
    public Prg(int a)
    {
        this();
        System.out.println("In single parameter constructor");
    }
    public Prg(int b, int c)
    {
        //Prg obj = new Prg(10, 20);

this(b);        System.out.println("In multiple parameter constructor");
    }
    public static void main(String s[])
    {
        Prg obj = new Prg(10, 20);
    }
}