java中super()和this()的区别

类似条款: 超级和这个关键词 super()和this()都用来制作 构造函数调用 .super()用于调用 基础 类的构造函数(即父类),而this()用于调用 现在的 类的构造函数。

null

让我们详细了解这两个方面:

超级()

  1. 超级() 用于调用基类(父类)的构造函数。

    // Java code to illustrate usage of super()
    class Parent {
    Parent()
    {
    System.out.println( "Parent class's No " +
    " arg constructor" );
    }
    }
    class Child extends Parent {
    Child()
    {
    super ();
    System.out.println( "Flow comes back from " +
    "Parent class no arg const" );
    }
    public static void main(String[] args)
    {
    new Child();
    System.out.println( "Inside Main" );
    }
    }

    
    

    输出:

    Parent class's No arg constructor
    Flow comes back from Parent class no arg const
    Inside Main
    

    项目流程:

    • 总的来说,我们已经发表了一项声明 新生儿() ,因此它调用子类的无参数构造函数。
    • 我们的内心 超级() 它调用父类的无参数,因为我们已经编写了super()和无参数,这就是为什么它调用父类的无参数构造函数,因为我们有一个SOP语句,因此它会打印 父类的无参数构造函数 .
    • 现在,随着父类的No-argument const完成,流返回到子类的No-argument,我们有一个SOP语句,因此它会打印出来 流从父类no arg const返回 .
    • 在完成子类流的无参数构造函数之后,现在再次返回main并执行剩余的语句和打印 内干道 .
  2. 我们可以使用super() 只有在构造函数内部,而不是其他地方 ,甚至在静态上下文中,甚至在方法和super()中都不应该 第一句话 内部构造函数。

    // Java program to illustrate usage of
    // super() as first statement
    class Parent {
    Parent()
    {
    System.out.println( "Parent class's No " +
    "arg constructor" );
    }
    }
    class Child extends Parent {
    Child()
    {
    // Uncommenting below line causes compilation
    // error because super() should be first statement
    // System.out.println("Compile Time Error");
    super ();
    System.out.println( "Flow comes back from " +
    "Parent class no arg const" );
    }
    public static void main(String[] args)
    {
    new Child();
    System.out.println( "Inside main" );
    }
    }

    
    

    输出:

    Parent class's No arg constructor
    Flow comes back from Parent class no arg const
    Inside main
    

    注: 超级()应该是 第一 任何构造函数中的语句。它可以被使用 仅在构造函数内部 没有别的地方。super()用来指代 只有父类(超类)的构造函数 .

这个()

  1. this()用于调用 当前类的构造函数 .

    // Java code to illustrate usage of this()
    class RR {
    RR()
    {
    this ( 10 );
    System.out.println( "Flow comes back from " +
    "RR class's 1 arg const" );
    }
    RR( int a)
    {
    System.out.println( "RR class's 1 arg const" );
    }
    public static void main(String[] args)
    {
    new RR();
    System.out.println( "Inside Main" );
    }
    }

    
    

    输出:

    RR class's 1 arg const
    Flow comes back from RR class's 1 arg const
    Inside Main
    

    程序流程 :

    • 首先从主要方面开始,然后在这方面我们已经做了陈述 新生儿() 因此,它调用子类的无参数构造函数,在我们 这(10) 它调用当前类(即RR类)的1参数
    • 因为我们写了这个(10)和一个参数,所以它调用RR类的一个参数构造函数。因为我们有一个SOP声明,所以它会打印出来 RR类的1参数常量 .
    • 现在,随着RR类的1参数const完成,所以流返回到RR类的no参数,我们有一个SOP语句,因此它会打印出来 流从RR类的1参数常量返回 .
    • 在完成RR类流的无参数构造函数之后,现在再次返回main并执行其余语句和打印 内干道 .
  2. 我们可以用这个 只有在构造函数内部,而不是其他地方 ,甚至在静态上下文中,甚至在方法内部都不是,this()应该是 第一句话 内部构造函数。

    // Java program to illustrate usage of
    // this() as first statement
    class RR {
    RR()
    {
    // Uncommenting below line causes compilation
    // error because this() should be first statement
    // System.out.println("Compile Time Error");
    this ( 51 );
    System.out.println( "Flow comes back from RR " +
    "class 1 arg const" );
    }
    RR( int k)
    {
    System.out.println( "RR class's 1 arg const" );
    }
    public static void main(String[] args)
    {
    new RR();
    System.out.println( "Inside main" );
    }
    }

    
    

    输出:

    RR class's 1 arg constructor
    Flow comes back from RR class 1 arg const
    Inside main
    

    注: 这个应该是 第一 任何构造函数中的语句。它可以被使用 仅在构造函数内部 没有别的地方。这个()是用来指 只有当前类的构造函数 .

关于this()和super()的要点

  1. 我们也可以使用super()这个() 只有一次 内部构造函数。如果我们使用super()两次或this()两次,或者super()后接this()或this()后接super(),那么我们会立即得到编译时错误,也就是说,我们可以使用 super()或this()作为构造函数中的第一个语句,而不是两者都是 .
  2. 这取决于你是否使用super()或this(),因为如果我们不使用this()或super(),那么 默认情况下,编译器将放入super() 作为构造函数中的第一条语句。

    // Java program to illustrate super() by default
    // executed by compiler if not provided explicitly
    class Parent {
    Parent()
    {
    System.out.println( "Parent class's No " +
    "argument constructor" );
    }
    Parent( int a)
    {
    System.out.println( "Parent class's 1 argument" +
    " constructor" );
    }
    }
    class Base extends Parent {
    Base()
    {
    // By default compiler put super()
    // here and not super(int)
    System.out.println( "Base class's No " +
    "argument constructor" );
    }
    public static void main(String[] args)
    {
    new Base();
    System.out.println( "Inside Main" );
    }
    }

    
    

    Output:
    Parent class's No argument constructor
    Base class's No argument constructor
    Inside Main
    

    项目流程:

    • 在主楼里面 新基地() 然后流向 无参数构造函数 基本阶级的。
    • 之后如果我们不放super()或this(),那么 默认情况下,编译器put super() .
    • 因此,流量流向 父类的无参数构造函数 而不是一个参数构造函数 .
    • 然后打印出来 父类的无参数构造函数 .
    • 之后,当Parent()构造函数完成时,再次流动 回来 对此 基类的无参数构造函数 并执行下一个SOP语句,即, 基类的无参数构造函数 .
    • 完成后,没有参数构造函数流出现 返回main() 再次打印main()中的剩余语句, 内干道

    但是,如果显式指定,可以在super之前使用this()。

    // Java program to illustrate super() put by
    // compiler always if not provided explicitly
    class Parent {
    Parent()
    {
    System.out.println( "Parent class's No " +
    "argument constructor" );
    }
    Parent( int a)
    {
    System.out.println( "Parent class's one " +
    " argument constructor" );
    }
    }
    class Base extends Parent {
    Base()
    {
    this ( 10 );
    System.out.println( "No arg const" );
    }
    Base( int a)
    {
    this ( 10 , 20 );
    System.out.println( "1 arg const" );
    }
    Base( int k, int m)
    {
    // See here by default compiler put super();
    System.out.println( "2 arg const" );
    }
    public static void main(String[] args)
    {
    new Base();
    System.out.println( "Inside Main" );
    }
    }

    
    

    输出:

    Parent class's No argument constructor
    2 arg const
    1 arg const
    No arg const
    Inside Main
    
  3. 不允许递归构造函数调用

    // Java program to illustrate recursive
    // constructor call not allowed
    class RR {
    RR()
    {
    this ( 30 );
    }
    RR( int a)
    {
    this ();
    }
    public static void main(String[] args)
    {
    new RR();
    }
    }

    
    

    输出:

    Compile time error saying recursive constructor invocation
    

    项目流程: 在这里,上面从main()开始,然后流到 没有RR类的参数构造函数 .之后我们有 这(30) 然后流向 RR的1个参数构造函数 在这方面我们有 这个() 所以流再次转到基类的无参数构造函数,在这里我们又得到了这个(30),流再次转到基类的1参数构造函数,并且 事情还在继续 ……就像一个 递归 .所以它是无效的这就是为什么我们 获取编译时错误 递归构造函数调用 .所以递归构造函数调用 java中不允许 .

本文由 拉贾特·拉瓦特 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 贡献极客。组织 或者把你的文章寄到contribute@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。

如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享