Ich möchte mehrere Konstruktoren ausführen, während ich ein einzelnes Objekt erstelle. Zum Beispiel habe ich eine Klassendefinition wie this-So führen Sie mehrere Konstruktoren aus, wenn Sie ein einzelnes Objekt erstellen
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");
}
}
Und ich versuche es durch den folgenden Code zu erreichen -
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);
}
}
Aber in diesem Fall ist es erzeugt Fehler wie -
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
Dank
dieser Artikel kann auch hilfreich sein: http: //www.yegor256. com/2015/05/28/one-primary-constructor.html – yegor256