• 【Java面向对象】继承的认识与实现(2) 关键字 this 与 super 区别


    CSDN话题挑战赛第2期
    参赛话题:学习笔记

    this 的初步认识

    Q:什么是 this 关键字


    A: this是当前对象的引用,就是说当前用构造函数建的对象是谁,这个this就代表谁,它是一个引用。


    Q:怎么使用 this 关键字

    我们来看下面这个案例。

    public class Text {
      public static void main(String[] args) {
           student stu = new student("cc");
           stu.info();
       }
    }
    
    class student {
       String name;
       public student(String sname) {
           name = sname;
       }
       public void info() {
           System.out.println(name);
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这个案例里面,我们通过调用构造器来对name赋值,由于snamename不能相同,这就让构造器看起来就很奇怪,有没有什么方法可以把构造器的形参直接写成属性名呢。


    我们使用this关键字就可以了!

    public class Text {
      public static void main(String[] args) {
           student stu = new student("cc");
           stu.info();
       }
    }
    
    class student {
       String name;
       public student(String name) {
           this.name = name;
       }
       public void info() {
           System.out.println(name);
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    Q: this 关键字有什么用处

    1. this.data : 用来访问本类的成员方法。
      举个例子(上面那个就是)

    1. this.func(): 用来在普通成员方法中调用另一个成员方法。
      举个例子
    public class Text {
      public static void main(String[] args) {
           student stu = new student("cc");
           stu.Class();
       }
    }
    
    class student {
       String name;
       public student(String name) {
           this.name = name;
       }
       public void Class() {
           System.out.println(name+"上课");
           this.Homework();
       }
       public void Homework() {
           System.out.println(name+"正在写作业");
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    1. this(): 用来访问本类的成员方法。
      举个例子
    public class Text {
       public static void main(String[] args) {
           student stu = new student();
           stu.show();
       }
    }
    
    class student {
       String name;
       public student() {
           this("cc");
       }
       public student(String name) {
           this.name = name;
       }
       public void show() {
           System.out.println(name);
       }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    注意: this() 不能使用在普通方法中 只能写在构造方法中,而且必须是构造方法中的第一条语句。

    super 的初步认识

    Q:什么是 super 关键字


    A: supejava的子类中指代父类引用。


    Q: super 关键字的作用

    1. 调用父类被子类重写的方法
    2. 调用父类被子类重定义的字段(被隐藏的成员变量)
    3. 调用父类的构造方法

    Q: super 关键字的特点

    我们来看下面这个案例。

    class Animal {
    	//构造函数
    	public Animal() {
    		System.out.println("Animal类的无参数构造函数执行");
    	}
    }
    
    class Cat extends Animal{
    	//构造函数
    	public Cat() {
    		System.out.println("Cat类的无参数构造函数执行");
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    当我们new一个新的cat类的时候,会出现运行结果:

    Animal类的无参数构造函数执行
    Cat类的无参数构造函数执行
    
    • 1
    • 2

    为什么呢?
    当子类的构造方法内第一行没有出现“super()”时,系统会默认给它加上无参数的"super()"方法。上面的例子就相当于:

    class Animal {
    	//构造函数
    	public Animal() {
    		System.out.println("Animal类的无参数构造函数执行");
    	}
    }
    
    class Cat extends Animal{
    	//构造函数
    	public Cat() {
    		super();
    		System.out.println("Cat类的无参数构造函数执行");
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    this 与 super 区别

    1. thissuper一样,都是对象内部的引用变量,只能出现在对象内部;
    2. this指向当前对象自己,super指向当前对象的父类型特征。
    3. this()super()都只能出现在构造方法的第一行,因此this()和super()方法不能共存,当一个类的构造方法第一行中没有this(),也没有super(),系统默认有super()方法;
    4. this()是构造方法中调用本类其他的构造方法,super()是当前对象构造方法中去调用自己父类的构造方法。
  • 相关阅读:
    【数据结构】链表相关OJ题 (万字详解)
    react-route的路由
    sketch有哪些好用的技巧,很少人知道
    JavaSE学习----(七)JDK类库的根类:Object
    Vue 和 jQuery 两者之间的区别是什么?
    swift语言用哪种库适合做爬虫?
    python3.12取消setuptools核心包,在pycharm中无法使用virtualenv进行虚拟环境的创建。
    【kubernetes】Debian使用Kubeadm部署Kubernetes失败:Connection Refused
    详解RFC 793文档-1
    基于java+springboot+mybatis+vue+elementui的准妈妈孕期交流平台
  • 原文地址:https://blog.csdn.net/Ceylan__/article/details/127131273