使用this()的Java构造函数链接

使用this()的Java构造函数链接,java,Java,如果在构造函数链接场景中,我想在子类构造函数中使用参数化的super()在this()之前调用super类的参数化构造函数,那么如何编码?? 因为在子类构造函数中,在this()之前不会提到super()而会自动调用超类默认构造函数,但是如果我想调用在super()中提供参数的参数化构造函数,那么如何调用呢?因为明确地说,它不允许在this()之前提到super()。 请提前回复,谢谢 class Parent { Parent(){ System.out.println(“欢迎加入父类的默

如果在构造函数链接场景中,我想在子类构造函数中使用参数化的
super()
this()
之前调用super类的参数化构造函数,那么如何编码?? 因为在子类构造函数中,在
this()
之前不会提到
super()
而会自动调用超类默认构造函数,但是如果我想调用在
super()
中提供参数的参数化构造函数,那么如何调用呢?因为明确地说,它不允许在
this()
之前提到
super()
。 请提前回复,谢谢

class Parent {
Parent(){
System.out.println(“欢迎加入父类的默认构造函数”);
} 
父(int x){
System.out.println(“欢迎加入父类的参数化构造函数”);
System.out.println(“你的幸运号是:“+x”);
} 
} 
类子级扩展父级
{
{
System.out.println(“我在init块中”);
}
Child()
{ 
//超级(7);错误!!
这(10);
System.out.println(“我在构造函数子()中”);
}
儿童(int x)
{ 
这(20,30);
System.out.println(“我在构造函数子(intx)”中);
}
子(整数x,整数y)
{
System.out.println(“我在构造函数子(intx,inty)”中);
}
公共静态void main(字符串arg[])
{
System.out.println(“我在主界面”);
新生儿();
System.out.println(“我又在主站了”);
}
}

}
构造函数既不能隐式也不能显式地同时使用
super()
this()
调用。您可以链接到超级类构造函数(
super()
),也可以链接到同一个类构造函数(使用
this()


因此,如果您想调用参数化的超类构造函数,只需这样做,而不必提及
this()
call。它会起作用。

构造函数既不能隐式使用,也不能显式使用
super()
this()
调用。您可以链接到超级类构造函数(
super()
),也可以链接到同一个类构造函数(使用
this()


因此,如果您想调用参数化的超类构造函数,只需这样做,而不必提及
this()
call。这会有用的。

请尝试下面的课程

public class test2 {
    public static void main(String a[]){
        test1 tst1 = new test1(1,2,3);

    }

public class test1 {
    int a;
    int b;
    int c;

    public static void main(String[] args) {

    }

    public test1(){
        System.out.println("am in default const");
    }

    public test1(int a){
        this();
        this.a=a;
        System.out.println("am in one param const");
    }

    public test1(int a,int b){
        this(a);
        this.b=b;
        System.out.println("am in two param const");
    }

    public test1(int a,int b,int c){
        this(a,b);
        this.c=c;
        System.out.println("am in three param const");
    }

请尝试下面的课程

public class test2 {
    public static void main(String a[]){
        test1 tst1 = new test1(1,2,3);

    }

public class test1 {
    int a;
    int b;
    int c;

    public static void main(String[] args) {

    }

    public test1(){
        System.out.println("am in default const");
    }

    public test1(int a){
        this();
        this.a=a;
        System.out.println("am in one param const");
    }

    public test1(int a,int b){
        this(a);
        this.b=b;
        System.out.println("am in two param const");
    }

    public test1(int a,int b,int c){
        this(a,b);
        this.c=c;
        System.out.println("am in three param const");
    }

由您从子构造函数调用super()。 如果您不在子构造函数中提到super,那么在这种情况下,将使用super()调用父构造函数的默认构造函数。但是,如果要调用父级的参数化构造函数,请使用super(传递与父级构造函数签名匹配的参数)

请参阅以下代码:

class Parent {
  Parent() {
    this(30);
    System.out.println("Welcome in Parent class'Default Constructor"); 
  } 
  Parent(int x) {
    System.out.println("Welcome in Parent class'Parameterized Constructor"); 
    System.out.println("Your Lucky no.is: "+x); 
  } 
}


public class Child extends Parent{


  Child(){
    this(25);
  }

  Child(int x){
    super(x);
  }

  public static void main(String arg[]){
    Child child= new Child();

    Child child1= new Child(20);


  }
}
输出:

Welcome in Parent class'Parameterized Constructor
Your Lucky no.is: 25
Welcome in Parent class'Parameterized Constructor
Your Lucky no.is: 20
在父构造函数中,您会注意到从默认构造函数调用参数化构造函数。在这(30)的帮助下,这是可能的


编辑:不能同时使用super()和this()。

由您从子构造函数调用super()。 如果您不在子构造函数中提到super,那么在这种情况下,将使用super()调用父构造函数的默认构造函数。但是,如果要调用父级的参数化构造函数,请使用super(传递与父级构造函数签名匹配的参数)

请参阅以下代码:

class Parent {
  Parent() {
    this(30);
    System.out.println("Welcome in Parent class'Default Constructor"); 
  } 
  Parent(int x) {
    System.out.println("Welcome in Parent class'Parameterized Constructor"); 
    System.out.println("Your Lucky no.is: "+x); 
  } 
}


public class Child extends Parent{


  Child(){
    this(25);
  }

  Child(int x){
    super(x);
  }

  public static void main(String arg[]){
    Child child= new Child();

    Child child1= new Child(20);


  }
}
输出:

Welcome in Parent class'Parameterized Constructor
Your Lucky no.is: 25
Welcome in Parent class'Parameterized Constructor
Your Lucky no.is: 20
在父构造函数中,您会注意到从默认构造函数调用参数化构造函数。在这(30)的帮助下,这是可能的



编辑:您不能同时使用super()和this()。

向我们展示一些代码来说明您的想法。请。不要将代码放在注释中。@Tichodroma我想在我需要的子类中使用构造函数链接,然后访问父类参数化构造函数。现在该怎么办???给我们看一些代码来说明你的想法。请。不要将代码放在注释中。@Tichodroma我想在我需要的子类中使用构造函数链接,然后访问父类参数化构造函数。现在该怎么做???你的代码告诉我们什么?这是一个示例,可以帮助人们理解构造函数调用,使用它。你介意一些解释吗?请执行代码,你应该能够从生成的输出中理解,如果你愿意,我仍然可以解释。Child(){//super(7);错误无法授予此(10)权限;System.out.println(“构造函数子()”)}您的代码告诉了我们什么?这是一个示例,可以帮助人们理解构造函数调用,使用它。您介意一些解释吗?请执行代码,您应该能够从生成的输出中理解,但如果您愿意,我可以解释。Child(){//super(7);错误无法授予此(10)权限;System.out.println(“构造函数子()”)}您的代码只显示继承,但我希望在继承父类时使用构造函数链接。请参考我编辑的答案。但您没有在我需要的子类中使用构造函数链接,然后访问父类参数化构造函数。现在怎么办???编辑了答案。请注意,您不能同时使用super和this。如果