• 【 java 面向对象】this和super关键字


    📋 个人简介

    • 💖 作者简介:大家好,我是阿牛,全栈领域新星创作者。😜
    • 🎉 支持我:点赞👍+收藏⭐️+留言📝
    • 📣 系列专栏:重走 java 路🍁
    • 💬格言:要成为光,因为有怕黑的人!🔥
      请添加图片描述

    前言

    这一节复习一下this和super的使用,重点放在他们调用构造器上!

    this

    this 关键字的使用

    1.this可以用来修饰、调用:属性、方法、构造器

    2.this修饰属性和方法:
    this 理解为:当前对象或当前正在创建的对象

    • 在类的方法中,我们可以使用"this属性"或"this. 方法"的方式调用当前对象属性或方法。但是通常情况下,我们都选择省略"this ."。特殊情况下,如果方法的形参和类的属性同名时,我们必须显式的使用"this.变量"的方式,表明此变量是属性,而非形参。
    • 在类的构造器中,我们可以使用"this.属性"或"this.方法"的方式,调用当前正在创建的对象属性,但是通常情况下,我们都选择省略"this ."。特殊情况下,如果构造器的形参和类的属性同名时,我们必须显的使用"this.变量"的方式,表明此变量是属性,而非形参。
    package thissuper;
    
    public class Demo {
        String name;
        int age;
    
    
        public Demo() {
        }
        public Demo(int age){
    //        age = age; 错误写法!参数名和属性名相同,此时this不能省略
            this.age = age;
        }
        
        public void print(String name){
    //        name = name 错误写法!参数名和属性名相同,此时this不能省略
            this.name = name; 
            System.out.println("我是" + this.name);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    3.this调用构造器

    1. 我们在类的构造器中, 可以显式的使用"this (形参列表)"方式,调用本类中指定的其他构造器。
    2. 构造器中不能通过"this (形参列表)"方式调用自己。
    3. 如果一个类中有 n 个构造器,则最多有 n - 1 构造器中使用了"this (形参列表)"。
    4. 规定:"this (形参列表)"必须声明在当前构造器的首行。
    5. 构造器内部,最多只能声明一个"this (形参列表)",用来调用其他的构造器
    package thissuper;
    
    public class Demo {
            private String name;
            private int age;
    
    //    无参的构造方法
            Demo(){
            System.out.println("无参的构造方法");
            }
    //    一个参数的构造方法
            Demo(String n){
            this();
            this.name = n;
            System.out.println("一个参数的构造方法");
        }
    //    两个参数的构造方法
            Demo(String n,int a){
            this(n);
            this.age = a;
            System.out.println("两个参数的构造方法");
        }
        
            void speak() {
            System.out.println("我的名字是" + name + ",今年" + age + "岁");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    在这里插入图片描述

    super

    super的使用

    调用属性和方法
    1.我们可以在子类的方法或构造器中。通过使用"super.属性"或"super.方法"的方式,显式的调用父类中声明的属性或方法。但是,通常情况下,我们习惯省略"super ."

    2.特殊情况:当子类和父类中定义了同名的属性时,我们要想在子类中调用父类中声明的属性,则必须显式的使用"super.属性"的方式,表明调用的是父类中声明的属性。

    3.特殊情况:当子类重写了父类中的方法以后,我们想在子类的方法中调用父类中被重写的方法时,则必须显式的使用"super.方法"的方式,表明调用的是父类中被重写的方法。

    4.super 调用构造器

    1. 我们可以在子类的构造器中显式的使用"super(形参列表)"的方式,调用父类中声明的指定的构造器。
    2. "super(形参列表)"的使用,必须声明在子类构造器的首行!
    3. 我们在类的构造器中,针对于 " this (形参列表)" 或 "super(形参列表)" 只能二选一,不能同时出现。
    4. 在构造器的首行,没有显式的声明"this (形参列表)"或"super(形参列表)",则默认调用的是父类中空参的构造。
    5. 在类的多个构造器中,至少有一个类的构造器中使用了"super(形参列表)",调用父类中的构造器。

    父类Person.java

    package thissuper;
    
    public class Person {
        String name;
        int age;
        int id;
    
        public Person() {
            this.id = 11;
            System.out.println("我是父类的空参构造!");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    子类Student.java

    package thissuper;
    
    public class Student extends Person{
        int id; //属性不会被重写,不用担心会被覆盖掉,即这里的id和父类的id不同
    
        public Student() {
            //在构造器的首行,没有显式的声明"super(形参列表)",则默认调用的是父类中空参的构造!即super()
            System.out.println("我是子类的空参构造!");
        }
    
        public Student(int id) {
            super(); // 调用父类构造器
            this.id = id;
            System.out.println("我是子类的有参构造!");
            System.out.println("父类id : " + super.id);
            System.out.println("子类id : " + this.id);
        }
    
        public static void main(String[] args) {
            Student stu1 = new Student();
    
            Student stu2 = new Student(12);
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    在这里插入图片描述

    结语

    如果你觉得博主写的还不错的话,可以关注一下当前专栏,博主会更完这个系列的哦!也欢迎订阅博主的其他好的专栏。

    🏰系列专栏
    👉软磨 css
    👉硬泡 javascript
    👉flask框架快速入门

  • 相关阅读:
    mysql引入表名称的注意事项
    BERT深度学习基准模型特点与应用
    【编程题】【Scratch三级】2019.12 判断奇偶数
    什么情况下适合使用静态路由?什么情况下适合使用动态路由?
    考研结束大半年,研0这些时间普通人的一些感触(附开学flag)
    vhost dpdk 共享内存
    linux下安装maven(详细图文教程)
    核爆!字节跳动算法大佬手写1000页数据算法笔记:Github已标星79k
    基于Nodejs的房屋租赁管理系统的设计和实现
    Educational Codeforces Round 151 (Rated for Div. 2)
  • 原文地址:https://blog.csdn.net/qq_57421630/article/details/126526083