this和super详解

this和super

this关键字

表示当前对象

提示:当一个类的属性(成员变量)名与访问该属性的方法参数名相同时,则需要使用 this 关键字来访问类中的属性,以区分类的属性和方法中的参数。

this.属性名

new 出的是哪个对象,表示的就是哪个对象


public class Person {
    
    String name = "父类";

    public void show(){
        System.out.println(this.name); //这个this
    }

}


public class Student extends Person {

    String name = "子类";

    public void show(String name){
        System.out.println(name); //形參
        System.out.println(this.name); //子类
        System.out.println(super.name); //父类
    }
}


import com.qh.person.Person;
import com.qh.person.Student;

public class Test {
    public static void main(String[] args) {

      Student student = new Student();
       //new 出的是Student的对象
      student.show("形參");//方法中的this 表示Student对象

      System.out.println("==============");

      Person person = new Student();
      //new 出的是Person的对象
      person.show();//方法中的this 表示Person对象
    }
}

this.方法名

this 关键字最大的作用就是让类中一个方法,访问该类里的另一个方法或实例变量。

表示对其调用当前方法的对象

public class Animal {

    public void eat(){
        System.out.println("大口吃");
    }

    public void sleep(){
//        Animal animal = new Animal(); //通过创建对象
//        animal.eat();
        this.eat(); //直接通过this调用eat()方法
        System.out.println("吃完睡");
    }
}
public class AnimalTest {

    public static void main(String[] args) {

        //创建 Animal 对象
        Animal animal = new Animal();
        animal.sleep();
    }
}

this 可以代表任何对象,当 this 出现在某个方法体中时,它所代表的对象是不确定的,但它的类型是确定的,它所代表的只能是当前类的实例。只有当这个方法被调用时,它所代表的对象才被确定下来,谁在调用这个方法,this 就代表谁。

this() 访问构造方法

public class Student {

    String name ;
    int age;

    public Student(String name, int age) {
        System.out.println("调用了两个参数构造函数");
        this.name = name;
        this.age = age;
    }
    public Student(String name) {
        this.name = name;
        System.out.println("调用了name参数的构造函数");
    }
    public Student(int age) {
        this.age = age;
    }
    public Student() { //无参构造函数

//      this("张三"); //调用对应的构造函数
        this("李四",20);

    }
    public void print(){
        System.out.println(name+"\t"+age);
    }

    public static void main(String[] args) {
        Student student = new Student(); //使用无参函数创建对象
        student.print();
    }
}

结果

调用了两个参数构造函数
李四	20

注意:

  • this( ) 不能在普通方法中使用,只能写在构造方法中。
  • 在构造方法中使用时,必须是第一条语句。

super关键字

由于子类不能继承父类的构造方法,因此,如果要调用父类的构造方法,可以使用 super 关键字。super 可以用来访问父类的构造方法、普通方法和属性。

super 关键字的功能:

  • 在子类的构造方法中显式的调用父类构造方法,super( ) 必须是在子类构造方法的方法体的第一行。
  • 访问父类的成员方法和变量。

super访问父类的成员属性

这里不重新举例了,刚开始已经测试了super访问父类成员变量

super访问父类成员方法

class Person {
    void message() {
        System.out.println("This is person class");
    }
}
class Student extends Person {
    void message() {
        System.out.println("This is student class");
    }
    void display() {
        message();
        super.message();
    }
}
class Test {
    public static void main(String args[]) {
        Student s = new Student();
        s.display();
    }
}

super调用父类构造方法

public class F {

    public F() {
        System.out.println("F is init");
    }

    public F(int i) {
        System.out.println("F is init int");
    }
}
public class S extends F {

    public S() {
        // super(); // 缺省的有这么一行 访问了父类的构造函数
        super(10); // 调用了父类的有参构造函数重载
        System.out.println("S is init");
    }
}
public class Test {
    public static void main(String[] args) {

        new S(); // 创建子类对象之前先创建父类对象
    }
}

super和this的区别

this 指的是当前对象的引用,super 是当前对象的父对象的引用。下面先简单介绍一下 super 和 this 关键字的用法。

super 关键字的用法:

  • super.父类属性名:调用父类中的属性
  • super.父类方法名:调用父类中的方法
  • super():调用父类的无参构造方法
  • super(参数):调用父类的有参构造方法

如果构造方法的第一行代码不是 this() 和 super(),则系统会默认添加 super()。
this 关键字的用法:

  • this.属性名:表示当前对象的属性
  • this.方法名(参数):表示调用当前对象的方法

当局部变量和成员变量发生冲突时,使用this.进行区分。

关于 Java super 和 this 关键字的异同,可简单总结为以下几条。

  1. 子类和父类中变量或方法名称相同时,用 super 关键字来访问。可以理解为 super 是指向自己父类对象的一个指针。在子类中调用父类的构造方法。
  2. this 是自身的一个对象,代表对象本身,可以理解为 this 是指向对象本身的一个指针。在同一个类中调用其它方法。
  3. this 和 super 不能同时出现在一个构造方法里面,因为 this 必然会调用其它的构造方法,其它的构造方法中肯定会有 super 语句的存在,所以在同一个构造方法里面有相同的语句,就失去了语句的意义,编译器也不会通过。
  4. this( ) 和 super( ) 都指的是对象,所以,均不可以在 static 环境中使用,包括 static 变量、static 方法和 static 语句块。
  5. 从本质上讲,this 是一个指向对象本身的指针, 然而 super 是一个 Java 关键字。

参考:http://c.biancheng.net/java/80/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值