java的super详解

super详解

什么是super?

1.super怎么用?

  1. 在讲解之前,先建立三个程序,一个是主程序,一个是student类(子类),另一个是person类(父类)。
  2. 咱们的主程序肯定是要调用student类的,student类是继承person类的。
  3. 咱们先看一下这三个程序的代码。

2.调用属性的时候

  • 先看看person(父类)
public class person {
protected String name="gao";
}
  • 然后写个student(基类)
public class student extends person{
private String name="GAO";
public void test(String name){    //这地有个形参,表示需要主程序里面的实参给他传递参数。
    System.out.println(name);
    System.out.println(this.name);  //this表是输出本类的name
    System.out.println(super.name);// super则表示输出person(父类)的name.
}

}
  • 然后写个主程序
public class application {
    public static void main(String[] args) {
        student student = new student();
 student.test("GAOming");    //这个GAOming 表示是实参,传给text方法的形参。
    }
}
===================================================
那么他的最终输出结果就是
    Gaoming
    GAO
    gao

  1. 通过这个程序不难发现super和this的区别。

3.调用方法的时候

咱们再看一下用调用方法的时候一些问题。

  • person类(父亲)
public class person {
public void say(){
    System.out.println("person");
}
}

  • student类(子类)
public class student extends person{
public void say(){
    System.out.println("student");
}
    public void test1(){
       say();        //这个既没有this和super,它默认的是调用本类的方法。
       this.say();   //使用了this他肯定是调用了本类的方法。
       super.say();  //使用了super他肯定是调用了父类的方法。
    }
}

  • 主程序
public class application {
    public static void main(String[] args) {
        student  student = new student();
        student.test1();
    }
}
========================================
    他的最终出结果
    student
    student
    person

也就是说,没有加this和super的时候,他是默认调用本类的方法。

  • 咱们现在将父类的方法改成,private,代码如下
public class person {
private void say(){
    System.out.println("person");
}
}

此时主程序和student类(子类)是爆红的,也就是说他不能运行。可以得出一个结论:私有的东西无法被继承。

4.探究一下无参构造器在里面是怎么运行的

  1. 咱们之前说过,调用一个对象的时候,他是先走无参构造器的。那么咱们给无参构造器加一个 输出。
  • person(父类)
public class person {
    public person(){
        System.out.println("person无参构造器在这");
    }
}

  • studen(子类)
public class student extends person{
    public student(){
        System.out.println("student无参构造器在这");
    }
}

  • 主程序
public class application {
    public static void main(String[] args) {
        student student = new student();

    }
}
==========================================
    他的最终输出结果是
    person无参构造器在这
    student无参构造器在这

我们只是new了一个student类的对象,他却也罢person类的无参构造输出了,也就是说再student(子类)里面有一个隐藏代码,位置如下

  • 子类
public class student extends person{ 
    public student(){
        //隐藏代码:调用了父类的无参构造。
        super(); //若要显示定义,他必须放student类的无参构造里面的最前面,在他前面不能有任何东西,否则会报错。
        System.out.println("student无参构造器在这");
    }
}

也就是调用父类的构造器,必须要在子类的第一行。

  1. 咱们之前还说过:当你写了一个有参构造的时候,无参构造就没了,那么这种情况下会怎么运行。

此时咱们再输入这个子类代码,他就会爆红。

public class student extends person{ 
    public student(){   //public student 爆红
        super();      //super(); 爆红
        System.out.println("student无参构造器在这");
    }
}

因为父类里面的无参构造没有了,不管写不写super();这个子类的无参构造都会爆红。

那么我们就是想用子类的无参构造,咱们需要在子类的无参构造,写上super(“name”);即调用父类的有参构造,那么这个子类的无参构造就不会爆红了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GaoJa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值