this和super

前言:理论代码必须结合起来才能真正的掌握

一、this

概念:this代表着当前对象的引用,也是当前函数所属对象的引用。直白的说,哪个对象调用了当前函数,this就代表哪个对象。

常见的用法(理论不理解就先参考下面案例)

  1. 最常见的情况是是对象的一个属性或被构造器的参数屏蔽时,如果需要调用屏蔽的属性,this就代表哪个对象
  2. this只能在方法内使用,表示正在调用方法的那个对象,但是,如果在方法内调用同一个类的另一个方法,就不必使用this,直接调用即可,this关键字是能省则省
  3. this和static的关系:
    static方法是类方法,依附于类而不依赖与任何对象,static属性是指该属性是类中所有对象所共享的,static方法是类方法,先于任何实例(对象)存在,static在类加载时就已经存在了,但对象是在创建时才生成;方法中使用this关键字它的值是当前对象的引用,只能用它调用属于当前对象的属性和方法和。但是,this可以调用static类型的属性,举个例子:一个父亲是不可能向他还未出生的孩子借钱的,但孩子出生后完全可以找他父亲去借钱;
  4. 对于一个类中的this,不一定单指这个的对象,也可能是这个类的子类的对象(抽象类里面的this只能是实际调用中它的派生类的实例化对象);
    总之:如果new 父类对象的话,父类方法的this指向的是父类,如果new 子类,那么父类方法的this指向的是子类
  5. this关键字也可以用于在构造函数中调用其他构造函数。但是,只能定义在构造函数的第一行,因为初始化动作要先执行

this使用案例

class Student extends Person{

    public Student(String name, int age, String nation) {
        super(name);
    }
}

public class Person{
    private String name;
    private int age;
    private static String nation = "chinese";

    public Person(String name) {
        this.name = name;
    }

    public Person(String name, int age) {
//        初始化方法必须放到第一行
        this(name);
        this.age = age;
    }

    //局部变量name和age屏蔽了name和age属性
    public Person(String name, int age,String nation) {
        this.name = name;
        this.age = age;
        //this可以调用到静态属性
        this.nation = nation;
    }


    public void test(){
        System.out.println("测试");
    }

    public static void test2(){
        //静态方法内不能出现this或super
        System.out.println("测试");
    }

    public void print(){
        //调用类内部的方法(toString已重写)加不加this都行
        this.toString();
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    public static void main(String[] args) {
         //这个this代表的student对象
         Student student = new Student("wzh",20,"chinese2");
         student.toString();
         //这个this代表的是person对象
         Person person = new Person("wzh2",20,"chinese2");
         student.toString();

    }

}

结果:
在这里插入图片描述

二、super

概念:
super可以理解为“父类的”,super可以在子类中调用父类的属性,方法,构造器,super关键字和this一样能省就省;

注意点:
1、this和super很像,this指向的是当前对象的调用,super指向的是当前调用对象的父类
2、类加载完毕,创建对象,父类的构造方法会被调用

  • 父类如果重写了无参构造器或者父类中没有有参构造器,那么子类的构造方法第一行就是super(),可以省略
class Student extends Person{
    //这是默认的构造器内容,写出来是为了帮大家理解
    public Student(){
        super();
    }
}

public class Person{
    private String name;
    private int age;
}
1234567891011
  • 如果父类中定义了有参构造器但没有显示写出无参构造器,那么必须通过super调用父类的有参构造函数,如果父类中定义了多个有参构造区,那么用super调用其中一个有参构造器即可
class Student extends Person{

    public Student(String name, int age) {
        //任选一个父类有参构造
        //super(name, age);
        super(name);
    }
}

public class Person{
    private String name;
    private int age;

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

    public Person(String name) {
        this.name = name;
    }
}

3、子类相应构造创建了一个子类对象,该子类对象还包含了一个父类对象。该父类对象在子类对象内部(正如子类的构造器无论如何都要通过super调用父类构造器一样,子类的对象被成功构造,那么它的内部就会包含父类的对象),证明如下

  • 子类重写父类的方法后可以通过super调用到父类的方法
class Student extends Person {
    private String name = "wzh2";

    @Override
    public String getName() {
        return "子类" + name;
    }

    public String getParentName(){
        //调用父类的方法
        return super.getName();
    }

    public static void main(String[] args) {
        Student student = new Student();
        System.out.println(student.getName());
        System.out.println(student.getParentName());
    }
}

public class Person{
    //protected意味着子类和同一包中可以访问
    protected String name = "wzh"; 
    protected int age = 20;

    public String getName() {
        return "父类" +name;
    }
}

输出结果
在这里插入图片描述

  • 子类获取到父类的变量
class Student extends Person{

    public void parentDisplay(){
        System.out.println(super.age + super.name);
    }

    public static void main(String[] args) {
        new Student().parentDisplay(); //输出结果:20wzh
    }
}

public class Person{
    //protected意味着子类和同一包中可以访问
    protected String name = "wzh";
    protected int age = 20;
}    
  • 调用父类的构造器
    不再举例

4、this super只能在有对象的前提下使用,不能在静态上下文使用
5、如果一个类没有基础任何父类,super还有用吗?肯定有用,可以调用Object中的方法

public class Person{
    private String name;
    private int age;

    public void display(){
        //通过this或super调用到了Object的toString();
        System.out.println(super.toString());
    }

    public static void main(String[] args) {
        new Person().display(); //输出为Person@452b3a41
    }

}

文章转自:挖坑埋你的王子晗—>https://blog.csdn.net/wwwwwww31311/article/details/113281877?utm_medium=distribute.pc_category.none-task-blog-hot-14.nonecase&depth_1-utm_source=distribute.pc_category.none-task-blog-hot-14.nonecase&request_id=

  • 2
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值