Java关键字 —— super 与 this 详细解释!一看就懂 有代码实例运行!

🧸欢迎来到dream_ready的博客,📜相信您对博主首页也很感兴趣o (ˉ▽ˉ;)

📜https://blog.csdn.net/dream_ready?spm=1010.2135.3001.5421

目录

前言

super关键字

1、访问父类的成员变量

2、调用父类中的构造方法

3、调用父类中的方法

this关键字

this和super的区别


前言

大家应该经常会遇到这static、this、super、final四个关键字,甚至容易把含义搞混。

其实static、this、super、final这四个关键字互相是没有什么联系的(除了this和super有一定关系外),所以大家其实要刻意区分一下这几个对应的功能和使用场景

那么这篇博客带领大家学习一下 super 和 this 的用法

在这里大家可以简单这样理解:this表示当前类的...,super表示父类的...

下面我们来详细讲解 super 和 this 两个关键字各自的用法和区别

super关键字

 super在父类、子类相关联的操作中经常被用到

super 是Java中的关键字,用于引用父类(超类)的成员变量、方法或构造函数。它有以下主要用途:

  1. 访问父类中的成员变量
  2. 调用父类中的构造方法
  3. 调用父类中的方法

在没有继承和被继承关系的类中,几乎不被使用

我个人喜欢将super理解为 “父类的”, “引用父类的”

1、访问父类的成员变量

使用 super 关键字可以在子类中访问父类中的成员变量。这对于在子类中有相同名字的成员变量时很有用,以便明确指定你想要访问的是父类的成员变量。

package com.sky.test;

class Parent {
    int x = 10;
}

class Child extends Parent {
    int x = 20;

    void display() {
        System.out.println(super.x); // 访问父类的x变量    10 
        System.out.println(this.x);    // 访问子类的x变量  20
    }
}

public class Main {
    public static void main(String[] args) {
        Child child = new Child();
        child.display();
    }
}

class Animal {
    String name = "Animal"; // 父类的成员变量

    void printName() {
        System.out.println(name); // 打印父类的成员变量
    }
}

class Dog extends Animal {
    String name = "Dog"; // 子类的成员变量

    void displayNames() {
        System.out.println(name);     // 打印子类的成员变量
        System.out.println(super.name); // 打印父类的成员变量
    }
}

public class Main {
    public static void main(String[] args) {
        Dog myDog = new Dog();
        myDog.printName();    // 输出:Animal(调用父类方法)
        myDog.displayNames(); // 输出:Dog(子类成员变量),Animal(父类成员变量)
    }
}

 

2、调用父类中的构造方法

在子类的构造函数中使用 super 关键字可以调用父类的构造函数。这通常用于初始化父类的成员变量或执行父类构造函数的逻辑。

经常用在下面这种情况,类中有些变量是子类继承父类的

利用super可调用父类的构造方法将其赋值

class Parent {
    int x;

    Parent(int x) {
        this.x = x;
    }
}

class Child extends Parent {
    int y;

    Child(int x, int y) {
        super(x); // 调用父类构造函数  将x赋值
        this.y = y;
    }

    // @Override 不理解也没事,不带上这个注解一样能正常运行
    @Override  // java中的注解 此处的意思是表明此方法是重写过的方法
    public String toString() {   // 返回带上child的成员变量值的字符串 x和y
        return "Child{" +
                "y=" + y +
                ", x=" + x +
                '}';
    }
}

public class Main {
    public static void main(String[] args) {
        Child child = new Child(10, 20);
        System.out.println(child.toString());
    }
}

3、调用父类中的方法

使用 super 关键字可以在子类中显式调用父类的方法。这在子类重写父类的方法时特别有用,以便在子类中可以调用父类的版本。

class Parent {
    void print() {
        System.out.println("Parent's print method");
    }
}

class Child extends Parent {
    @Override
    void print() {
        super.print(); // 调用父类的print方法
        System.out.println("Child's print method");
    }
}


public class Main {
    public static void main(String[] args) {
        Child child = new Child();
        child.print();
    }
}

this关键字

其实this相对来说和super还是有一定关系的,this可以简单理解为 “当前类的”,super可以理解为 “父类的”

其实只要按着上面那句话记,大部分情况都可以得心应手的使用了

下面,我们继续来讲解this(主要讲解的是用法)

众所周知,this最常用的地方是在构造方法中使用,this.属性来给对象中的属性赋值,代码如下:

public class Account {
    private String name;
    private double balance;
    private String pwd;
	
	//Account类的一个构造器
	public Account (String name,double balance,String pwd){
		//构造器的实现---初始化对象
		this.name = name;
		this.balance = balance;	
		this.pwd = pwd;
	}
}

很多人都会有一个疑问,直接访问属性不是也可以访问到么,为什么要this.属性来访问呢

其实大家看上面的代码,方法传过来的参数命名和类中属性命名一摸一样,像这种情况,如果写成下面这种样子是谁赋值给谁呢?

public Account (String name,double balance,String pwd){
    //构造器的实现---初始化对象
    name = name;
    balance = balance;
    pwd = pwd;
}

像上面这种写法不仅本质上错误,而且含义太过于混淆

如果这时候引入this,就能区分开,this表示当前类的:

public Account (String name,double balance,String pwd){
    //构造器的实现---初始化对象
    this.name = name;
    this.balance = balance;
    this.pwd = pwd;
}

将方法接收的参数依次赋值给当前类的参数

this和super的区别

this表示当前类的,super表示父类的

官方一点就是:

  • this用于引用当前对象
  • super用于访问父类

 他俩可以在同一个方法中同时出现,各自发挥各自的作用,互不冲突,代码如下:

class animal {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

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

public class dog extends animal {
    private String color;

    public dog(String name, int age, String color) {
        super(name, age);
        this.color = color;
    }
}

在dog的构造方法中,使用了 super(name, age);调用了父类的构造方法,又使用了 this.color = color 为当前类的color赋值,作用互不冲突

🧸前路漫漫,愿星光与您相伴!

  • 24
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

dream_ready

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

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

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

打赏作者

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

抵扣说明:

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

余额充值