解析java中的super关键字

解析java中的super关键字

1 前提

继承本质

a 继承内存原理的结构图

在这里插入图片描述

b 示例代码
Animal类
public class Animal {
    private String  name="张三";
    private String type;
    private int age;
    public Animal() {

    }
    public Animal(String name, String type, int age) {
        this.name = name;
        this.type = type;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}


Cat类
public class Cat extends Animal{
    public Cat(){

    }
    public Cat(String name, String type, int age) {
        super(name, type, age);
    }

    @Override
    public String toString() {
       return "猫的名字为: "+getName()+",猫的种类为: "+getType()+",猫的年龄为: "+getAge();
    }
}

TestCat类
public class TestCat {
    public static void main(String[] args) {
        Animal animal=new Animal("李四","旺财",20);
        System.out.println(new Cat().getName());
       Cat c=new Cat("叮当","短尾",9);
        System.out.println(c.toString());
    }
}
c 示例代码运行截图

在这里插入图片描述

2 super关键字含义

super词义
在这里插入图片描述

子类继承父类后,会把父类的所有属性和方法都拷贝一份到自己实例化对象所在的堆空间中,我们把那块区域叫做父类型特征区,而super就是找到父类型特征区的路标,可以通过super.方式区访问其里面的内容.

简单来说,作用就在子类方法中调用子类中的父类型特征区的属性和方法去进行操作。如果修改里面的值,也仅仅是特征区的值在变化,并不会影响父类的中的相同名字属性的内容

3 为什么要使用super关键字?

访问并调用父类型特征区里面的属性和方法,使得子类可以使用继承到的属性和方法,若不写super.,其是先找堆空间里面有该属性和方法,若无再去找父类型特征区的属性和方法,写了可以节省一步我们人为已知的无效操作(寻找堆空间)。

4.如何使用super关键字

4.1 super.属性名

目的

得到父类型特征区中的属性值

示例代码
Animal类
public class Animal {
    private String  name="张三";
    private String type;
    private int age;
    public Animal() {

    }
    public Animal(String name, String type, int age) {
        this.name = name;
        this.type = type;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}


Cat类
public class Cat extends Animal{
    public Cat(){

    }
    public Cat(String name, String type, int age) {
        super(name, type, age);
    }

    @Override
    public String toString() {
       return "猫的名字为: "+getName()+",猫的种类为: "+getType()+",猫的年龄为: "+getAge();
    }
}

TestCat类
public class TestCat {
    public static void main(String[] args) {
        Animal animal=new Animal("李四","旺财",20);
        System.out.println(new Cat().getName());
    }
}

示例代码运行截图

在这里插入图片描述

4.2 super.方法名(实参列表)

目的

得到父类型特征区中的方法

示例代码
Animal类
public class Animal {
    private String  name="张三";
    private String type;
    private int age;
    public Animal() {

    }
    public Animal(String name, String type, int age) {
        this.name = name;
        this.type = type;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public void initalValue(){
        System.out.println("打印赋值副本时的初始值,名字为:"+name+",种类为: "+type+",年龄为:"+age);
    }

}


Cat类
public class Cat extends Animal{
    public Cat(){

    }
    public Cat(String name, String type, int age) {
        super(name, type, age);
    }

    @Override
    public String toString() {
       return "猫的名字为: "+getName()+",猫的种类为: "+getType()+",猫的年龄为: "+getAge();
    }
}

TestCat类
public class TestCat {
    public static void main(String[] args) {
        Animal animal=new Animal("李四","旺财",20);
        new Cat().initalValue();
    }
}
示例代码运行截图

在这里插入图片描述

4.3 super(实参列表)

目的

调用父类型特征区的构造方法,给父类型特征区里面的属性进行赋值

示例代码
Animal类
public class Animal {
    private String  name="张三";
    private String type;
    private int age;
    public Animal() {

    }
    public Animal(String name, String type, int age) {
        this.name = name;
        this.type = type;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    public void initalValue(){
        System.out.println("打印赋值副本时的初始值,名字为:"+name+",种类为: "+type+",年龄为:"+age);
    }

}


Cat类
public class Cat extends Animal{
    public Cat(){

    }
    public Cat(String name, String type, int age) {
        super(name, type, age);
    }

    @Override
    public String toString() {
       return "猫的名字为: "+getName()+",猫的种类为: "+getType()+",猫的年龄为: "+getAge();
    }
}

TestCat类
public class TestCat {
    public static void main(String[] args) {
        Animal animal=new Animal("李四","旺财",20);
        //会调用父类型特征的构造方法,对里面的属性进行赋值操作
        System.out.println(new Cat("叮叮当当", "加菲猫", 10).toString());

    }
}

示例代码运行截图

在这里插入图片描述

4.4 规则

a 子类继承父类时,构造方法里面没有写this();系统运行时会在首行自动加上super()关键字,也就是会默认先调用父类的无参构造方法(模拟先有父亲,再有儿子);
b.super()必须出在子类构造方法的第一行
c. super关键字是只能出现在实例方法中,不能出现在static修饰的方法中

原因:因为new创建空间之后才有super关键字,而static修饰的方法无需要new对象。因此不能出现在static修饰的方法中

d. super.可以出现在方法的任意行
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

SSS4362

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

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

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

打赏作者

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

抵扣说明:

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

余额充值