zoukankan      html  css  js  c++  java
  • Java 中 this 和 super 的用法总结

    this

    this 是自身的一个对象,代表对象本身,可以理解为:指向对象本身的一个指针

    this 的用法在 Java 中大体可以分为3种:

    1.普通的直接引用

    这种就不用讲了,this 相当于是指向当前对象本身。

    2.形参与成员名字重名,用 this 来区分:

    class Person {
        private int age = 10;
        public Person(){
        System.out.println("初始化年龄:"+age);
    }
        public int GetAge(int age){
            this.age = age;
            return this.age;
        }
    }
     
    public class test1 {
        public static void main(String[] args) {
            Person Harry = new Person();
            System.out.println("Harry's age is "+Harry.GetAge(12));
        }
    }

    运行结果:

    初始化年龄:10
    Harry's age is 12
    可以看到,这里 age 是 GetAge 成员方法的形参,this.age 是 Person 类的成员变量。

    3.引用构造函数

    这个和 super 放在一起讲,见下面。


    super

    super仅仅是能引用父类对象的属性,创建子类对象时也不会创建父类对象(super指向指向父类对象的说法不严谨)

    super 也有三种用法:

    1.普通的直接引用

    与 this 类似,这样就可以用 super.xxx 来引用父类的成员。

    2.子类中的成员变量或方法与父类中的成员变量或方法同名

    class Country {
        String name;
        void value() {
           name = "China";
        }
    }
      
    class City extends Country {
        String name;
        void value() {
        name = "Shanghai";
        super.value();      //调用父类的方法
        System.out.println(name);
        System.out.println(super.name);
        }
      
        public static void main(String[] args) {
           City c=new City();
           c.value();
           }
    }


    运行结果:

    Shanghai
    China

    可以看到,这里既调用了父类的方法,也调用了父类的变量。若不调用父类方法 value(),只调用父类变量 name 的话,则父类 name 值为默认值 null。

    3.引用构造函数

    • super(参数):调用父类中的某一个构造函数(应该为构造函数中的第一条语句)。
    • this(参数):调用本类中另一种形式的构造函数(应该为构造函数中的第一条语句)。
    • class Country {
          String name;
          void value() {
             name = "China";
          }
      }
        
      class City extends Country {
          String name;
          void value() {
          name = "Shanghai";
          super.value();      //调用父类的方法
          System.out.println(name);
          System.out.println(super.name);
          }
        
          public static void main(String[] args) {
             City c=new City();
             c.value();
             }
      }
    • 运行结果:

    父类·无参数构造方法: A Person.
    子类·调用父类”无参数构造方法“: A chinese coder.
    父类·含一个参数的构造方法: A person's name is codersai
    子类·调用父类”含一个参数的构造方法“: his name is codersai
    父类·含一个参数的构造方法: A person's name is codersai
    子类·调用父类”含一个参数的构造方法“: his name is codersai
    子类:调用子类具有相同形参的构造方法:his age is 18

    从本例可以看到,可以用 super 和 this 分别调用父类的构造方法和本类中其他形式的构造方法。

    例子中 Chinese 类第三种构造方法调用的是本类中第二种构造方法,而第二种构造方法是调用父类的,因此也要先调用父类的构造方法,再调用本类中第二种,最后是重写第三种构造方法。


    super 和 this的异同

    • super(参数):调用基类中的某一个构造函数(应该为构造函数中的第一条语句)
    • this(参数):调用本类中另一种形成的构造函数(应该为构造函数中的第一条语句)
    • super: 它引用当前对象的直接父类中的成员(用来访问直接父类中被隐藏的父类中成员数据或函数,基类与派生类中有相同成员定义时如:super.变量名 super.成员函数据名(实参) this:它代表当前对象名(在程序中易产生二义性之处,应使用 this 来指明当前对象;如果函数的形参与类中的成员数据同名,这时需用 this 来指明成员变量名)
    • 调用super()必须写在子类构造方法的第一行,否则编译不通过。每个子类构造方法的第一条语句,都是隐含地调用 super(),如果父类没有这种形式的构造函数,那么在编译的时候就会报错。
    • super() 和 this() 类似,区别是,super() 从子类中调用父类的构造方法,this() 在同一类内调用其它方法。
    • super() 和 this() 均需放在构造方法内第一行。
    • 尽管可以用this调用一个构造器,但却不能调用两个。
    • this 和 super 不能同时出现在一个构造函数里面,因为this必然会调用其它的构造函数,其它的构造函数必然也会有 super 语句的存在,所以在同一个构造函数里面有相同的语句,就失去了语句的意义,编译器也不会通过。
    • this() 和 super() 都指的是对象,所以,均不可以在 static 环境中使用。包括:static 变量,static 方法,static 语句块。
    • 从本质上讲,this 是一个指向本对象的指针, 然而 super 是一个 Java 关键字。
  • 相关阅读:
    windows下 php-cgi.exe 0xc000007b 错误 阿星小栈
    call to undefined function openssl cipher iv length() 报错 PHP7开启OpenSSL扩展失败 阿星小栈
    PHP json_encode返回的json前端获取时出现unicode转码和反斜杠导致无法解析的解决办法
    Warring:POST Content-Length of 625523488 bytes exceeds the limit of 8388608 bytes in Unknown on line 0 阿星小栈
    PHP数组分割成新数组 阿星小栈
    Laravel ajax请求419错误及解决办法(CSRF验证) 阿星小栈
    MySQL said: Authentication plugin 'caching_sha2_password' cannot be loaded... 阿星小栈
    Laravel框架发送邮件 阿星小栈
    PHP 导出Excel三种方式 阿星小栈
    mysql命令 出现ERROR 1054 (42S22): Unknown column 'password' in 'field list'
  • 原文地址:https://www.cnblogs.com/songhuiqiang/p/10644031.html
Copyright © 2011-2022 走看看