如何在Java项目中使用继承构造器

发布时间:2021-03-22 17:32:55 作者:Leah
来源:亿速云 阅读:154

本篇文章为大家展示了如何在Java项目中使用继承构造器,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

默认构造器

先看一段简单的测试代码:

package com.my.pac13;
/*继承中的构造*/
public class Person {
  Person(){
    System.out.println("Person()");
  }
}
class Student extends Person{
  Student(){
    System.out.println("Student()");
  }
}
class PrimaryStudent extends Student{
  PrimaryStudent(){
    //super();
    System.out.println("PrimaryStudent()");
  }
  public static void main(String[] args) {
    //创建了PrimaryStudent对象
    new PrimaryStudent();
  }
}
/*
 Person()
 Student()
 PrimaryStudent()
*/

关于构造器,我们前面提到,任何没有显式构造器的类都存在着一个无参数的默认构造器。我们上面的例子在默认构造器中加入了打印输出,以便理解。

可以看到的是:

在创建PrimaryStudent时,他的直接父类Student和间接父类Person中的构造器都被调用了,而且可以看到,是"自上而下"的。
父类在子类构造器可以访问它之前,就已经完成了初始化的操作。

若子类没有显式调用父类的构造器,则自动调用父类的默认(无参)构造器。

带参数的构造器

前面的代码中,每个类都含有默认的构造器,创建子类对象时,是自上而下,且子类会默认调用父类的无参构造器。那么,假设父类正好没有无参构造器或者你正想调用父类的带参构造器,这时就需要我们的super关键字。(super关键字之后还会进行总结)

我们直接在原来的基础上稍作修改,并进行测试。

package com.my.pac13;
/*调用基类构造器是子类构造器中要做的第一件事*/
public class Person {
  //没有默认构造器
  Person(String name){
    System.out.println("Person()\t"+name);
  }
}
class Student extends Person{
  //也没有默认构造器,且用super显式调用
  Student(String n){
  //super关键字调用父类的构造器
    super(n);
    System.out.println("一参数Student\t"+n);
  }
  Student(String n,String m){
  //this关键字调用同一类中重载的构造器
    this(n);
    System.out.println("二参数student()\t"+m);
  }
}
class PrimaryStudent extends Student{
  //隐式调用父类构无参数构造器,但是父类没有,所以要用super显式调用
  PrimaryStudent(){
  //没有下面的语句会报错
    super("hello");
    System.out.println("PrimaryStudent()");
  }

}
class ExtendsTest{
  public static void main(String[] args) {
    new Person("the shy");
    System.out.println("***********");
    new Student("rookie");
    System.out.println("***********");
    new Student("the shy","rookie");
    System.out.println("***********");
    new PrimaryStudent();
    System.out.println("***********");
  }

}
/*
Person()  the shy
***********
Person()  rookie
一参数Student rookie
***********
Person()  the shy
一参数Student the shy
二参数student()  rookie
***********
Person()  hello
一参数Student hello
PrimaryStudent()
***********
 */

子类调用父类构造器

无论是否使用super语句来调用父类构造器的初始化代码,子类构造器总是会事先调用父类构造器!这是一定要记住的!

子类构造器A在第一行显式使用super调用父类构造器B,格式super(参数列表),根据参数列表选择对应的父类构造器。

//父类
 Person(String name){
    System.out.println("Person()\t"+name);
  }
//子类
 Student(String n){
  //super关键字调用父类的构造器
    super(n);
    System.out.println("一参数Student\t"+n);
  }

子类构造器A先用this调用本类重载的构造器B,然后B调用父类构造器。

//父类
 Person(String name){
    System.out.println("Person()\t"+name);
  }
//子类
Student(String n){
  //super关键字调用父类的构造器
    super(n);
    System.out.println("一参数Student\t"+n);
  }
Student(String n,String m){
//this关键字调用同一类中重载的构造器
  this(n);
  System.out.println("二参数student()\t"+m);
}

子类构造器中没有super和this时,系统会隐式调用父类的无参构造器,要是没有无参的,那就报错。

//隐式调用父类构无参数构造器,但是父类没有,所以要用super显式调用
PrimaryStudent(){
//没有下面的语句会报错
  super("hello");
  System.out.println("PrimaryStudent()");
}

上述内容就是如何在Java项目中使用继承构造器,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. 如如何使用journalctl命令?
  2. 构造器继承

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:如何正确的使用@RequestBody

下一篇:使用split命令怎么对Linux文件进行分割

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》