• Java-this、super、static关键字


    this关键字


    this关键字强调调用本类中的方法,除此之外this还有如下:

    • 表示类中的属性
    • 调用本类的构造方法
    • this表示当前对象

    使用this调用本类的属性

    看以下程序

    public class Person {
        private String name;
        private int age;
    
        public Person(String name, int age) {
            name = name;
            age = age;
        }
    
        public Person() {
        }
    
        public static void main(String[] args) {
            Person person = new Person("name",18);
            System.out.println(person.name);
            System.out.println(person.age);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    并没有把数据传入构造方法中,这也就证明,现在构造方法不能把传递进去的赋值给类的属性,这种错误通过this关键字来解决。
    在这里插入图片描述加入this关键字
    在这里插入图片描述建议以后访问类的属性时候加上this关键字

    使用this调用构造方法

    如果一个类中有多个构造方法,也可以用this相互调用。

    下面使用this调用本类的构造方法

    public class Person {
        private String name;
        private int age;
    
    
        public Person(String name, int age) {
            this(); //在这调用person的午餐构造方法
            this.name = name;
            this.age = age;
        }
    
        public Person() {
            System.out.println("我是无参构造方法,我被调用了!");
        }
    
        public static void main(String[] args) {
            Person person = new Person("name",18);
            System.out.println(person.name);
            System.out.println(person.age);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述
    注意:构造方法是在实例化对象时调用的,也就是说,在类的所有方法中,无参构造方法是最先被调用的,也就解释了为什么无参构造方法会被放在构造方法的首行,因为是最先被调用。
    错误代码:

      public Person(String name, int age) {
            this.name = name;
              this(); //在这调用person的午餐构造方法
            this.age = age;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    注意:提供无参的构造方法作为程序的出口,否则任意造成构造方法的递归调用

    this表示当前对象

    在java中当前对象就是指当前正在调用类中的方法对象。

    class Person {
        public String getInfo(){
            System.out.println("person类->"+this);
            return null;
        }
    }
    public class ThisDemo{
        public static void main(String[] args) {
            Person p1 = new Person();
            Person p2 = new Person();
            System.out.println(p1);
            System.out.println("------------");
            p1.getInfo();
            System.out.println("------------");
            System.out.println(p2);
            p2.getInfo();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    static关键字

    使用static声明类属性

    static属性称为全局属性(有些也称为静态属性)

    class Person {
        private String name;
        private int age;
        static String country = "A城";
    
        public Person(String name, int age) {
            this.name = name;
            this.age = age;
        }
    
        public void info() {
            System.out.println(this.name);
            System.out.println(this.age);
            System.out.println(country);
        }
    }
    
    public class ThisDemo {
        public static void main(String[] args) {
            Person p1 = new Person("zhang",15);
            Person p2 = new Person("lisi",19);
            Person p3 = new Person("meimei",11);
            p1.info();
            p2.info();
            p3.info();
            System.out.println("-------");
            Person.country = "B城";
            p1.info();
            p2.info();
            p3.info();
    
        }
    }
    
    • 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
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    多个实例化对象共享一个属性,当属性改变时,全部实例化对象对应的也跟着改变。
    在这里插入图片描述

    栈内存空间:保存所有对象的名称
    堆内存空间:保存每个对象的具体属性内容
    全局数据区:保存static类型的属性
    全局代码区:保存所有的方法定义

    使用static声明方法

    static声明的方法称为类方法,可以直接用类对象名直接调用,不用实例化对象,注意非static的方法可以去调用static声明的方法或者属性,但是static声明的方法是不能调用非static的方法或者属性的。

    在这里插入图片描述
    因为在程序中所有的属性和方法必须在对象开辟堆内存之后才可以调用,而static类型的方法在对象没有实例化时就可以被类名直接掉调用。

    可以试着分析main方法,理解main方法。

    super关键字

    使用super关键字调用父类中指定的构造方法

    class Person{
        private String name;
        private int  age;
    
        public Person(String name, int age) {
            this.name = name;
            this.age = 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 String Info(){
            return this.getName() + "-" + this.getAge();
        }
    }
    
    class Studnet extends Person{
    
    
        private String school;
       public Studnet(String name, int age, String school){
           super(name,age);
           this.school=school;
    
       }
    
    
    
       public String getSchool(){
           return school;
       }
       public void setSchool(String school){
           this.school = school;
       }
    
    
        @Override
        public String Info(){
            return super.Info() + this.getSchool();
        }
    }
    
    public class OverrideDemo01{
        public static void main(String[] args) {
            Studnet studnet = new Studnet("zhangsan",30,"bbgu");
            System.out.println(studnet.Info());
        }
    }
    
    • 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
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62

    this跟super的比较

    thissuper
    访问本类的属性,如果本类没有从父类中查找访问父类的属性
    访本类的方法,如果本类没有从父类中查找直接访问父类的方法
    调用本类的构造方法,必须放在构造方法的首行调用父类的构造方法,必须放在子类构造方法的首行
    表示当前对象没有此概念
  • 相关阅读:
    springboot冬奥会竞赛项目知识网站的设计与实现毕业设计源码152337
    NVR新版界面看回放时音频功能如何开启
    计算机网路复习01
    linux NAT网卡配置static
    你不一定知道的七种进程间通信方式
    Linux的FTP服务
    深度学习计算 - GPU
    sql:group by和聚合函数的使用
    LM393 电压比较器及其典型电路介绍
    大学生必看!这些关乎你“钱途”的内容你一定要知道NISP一级证书
  • 原文地址:https://blog.csdn.net/qq_52166656/article/details/126747218