this与super的使用

this关键字与super关键字的使用说明

Author: Msuenb

Date: 2023-02-10


关键字-this

this是一个引用当前对象的变量,它的作用和其词义接近。this可以调用类的属性、方法(private属性和方法除外)构造器。

  • 在方法内部使用,即这个方法所属对象的引用。
  • 在构造器内部使用,表示该构造器正在初始化的对象。

this调用属性

this访问成员属性语法:

this.变量名;

this访问成员属性演示:

package com.atguigu.date0210.this_super;

public class ThisVarTest {
    public static void main(String[] args) {
        new Son1().method();
    }
}

class V {
    String a = "父类实例变量a";
    String b = "父类实例变量b";
}

class Son1 extends V {
    String a = "子类实例变量a";   // 与父类同名的 a

    public void method() {
        String a = "子类局部变量a";   // 与子类实例变量同名

        System.out.println(a);  // 就近原则 局部变量a
        System.out.println(this.a); // 子类实例变量 a

        System.out.println(this.b); // 子类中找不到 找父类中的 b
    }
}

this调用方法

this调用成员方法语法:

this.方法名(【参数列表】);

this调用成员方法演示:

public class ThisMethodTest {
    public static void main(String[] args) {
        new T().method();
        System.out.println("------------");
        new Son().method2();
    }
}

class T {
    public void fun1() {
        System.out.println("Father.fun1");
    }

    public static void fun2() {
        System.out.println("Father.fun2 static");
        // this.fun1(); // 静态方法中不能使用 this

        // 不要在这尝试用 this 调用静态方法 不行的
    }

    public void method() {
        fun1();  // 可以直接调用
        this.fun1();    // 也可以通过 this 调用
        this.fun2();    // 不推荐 静态方法使用 类名.方法名调用
    }
}

class Son extends T {
    public void method() {      // 重写父类的 method 方法
        fun1();    		
        this.fun1();    // 本类中找不到 找父类的 fun1 方法

        fun2();
        System.out.println("子类重写父类的method方法");
    }

    public void method2() {
        this.method();      // 调用的是子类的method方法
    }
}
  • 如果子类没有重写父类的方法,只有权限修饰符允许,在子类中完全可以直接调用父类的方法;
  • 如果子类重写了父类的方法,在子类中默认调用的子类重写的方法。需要通过super.才能调用父类被重写的方法

this调用构造器

this调用构造器语法:

this(【参数列表】);	// 只能在构造器中使用 且只能放在构造器的第一句 只能出现一句

this调用构造器演示:

public class ThisConstructorTest {
    public static void main(String[] args) {
        new C();
        new C("Tom");
        new C("Tom", 18);
    }
}

class C {
    private String name;
    private int age;
    private boolean isMarry;

    public C() {
        // this("Tom"); // 不能出现递归调用
        System.out.println("创建了新对象");
    }

    public C(String name) {
        this(); // 调用本类无参构造器
        this.name = name;
    }

    public C(String name, int age) {
        this(name); // 调用含有一个参数的构造器
        this.age = age;
    }

    public C(String name, int age, boolean isMarry) {
        this.isMarry = isMarry;
        // this(name, age); // this 调用构造器必须是第一条语句
        // 隐含 在一个构造器中使用 this 调用其他构造器的代码只能有一个
    }
}

关键字-super

Java类中可以使用super关键字来调用父类中的指定操作。super可以调用父类中定义的属性、方法和构造器。

  • 当子父类出现同名成员时(强烈不建议同名),可以使用super表明调用的是父类中的成员。
  • super的追溯不仅限于直接父类。

注意:this代表本类对象的引用;super代表父类的内存空间标识。this是变量,super不是。

super调用属性

super访问属性语法:

super.属性名;

super访问属性演示:

public class SuperVarTest {
    public static void main(String[] args) {
        new Son().method();
    }
}

class V {
    String a = "父类实例变量a";
    String b = "父类实例变量b";
}

class Son extends V {
    String a = "子类实例变量a";   // 与父类同名的 a

    public void method() {
        System.out.println(a);  // 子类的 a
        System.out.println(super.a);    // 父类的 a

        System.out.println(b); // 子类中找不到 找父类中的 b
        System.out.println(super.b);
    }
}

super调用方法

super调用方法语法:

super.方法名(参数列表)

super调用方法演示:

public class SuperMethodTest {
    public static void main(String[] args) {
        new Son().method2();
    }
}

class T {
    public void fun1() {
        System.out.println("Father.fun1");
    }

    public void method() {
        System.out.println("父类的method方法");
    }
}

class Son extends T {
    public void method() {      // 重写父类的 method 方法
        System.out.println("子类重写父类的method方法");
    }

    public static void fun() {
        // super.fun1();  // 静态方法中不能使用 super
    }

    public void method2() {
        super.fun1();

        method();   // 子类的method 方法
        super.method(); // 父类的method 方法
    }
}

super调用构造器

super调用构造器语法:

super(参数列表);  // 只能在构造器中使用 且只能放在构造器的第一句 只能出现一句

super调用构造器演示:

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

    }
}
class C {
    public int a;

    public C() {
        // super(); // 默认有 super(); C 是Object的子类
        System.out.println("C 无参构造器");
    }

    public C(int a) {
        // 默认有 super()
        this.a = a;
        System.out.println("C 有参构造器(int a)");
    }
}
class Son2 extends C{
    public int b;

    public Son2() {
        super();    // 明确调用父类的无参构造器
        // this(2);  super() 和 this() 只能有一个
        System.out.println("Son 无参构造器");
    }
    public Son2(int a){
        // 默认有 super()
        this.a = a;
        System.out.println("Son 有参构造器(int a)");
    }

    public Son2(int a, int b) {
        super(a);   // 明确调用父类的有参构造器
        this.b = b;
        System.out.println("Son 有参构造器(int a, int b)");
    }
}
  • 调用父类构造器的好处:分工明确,父类属性由父类初始化,子类属性由子类初始化。
  • 当子类中有和父类中成员重名时,为了访问父类的成员,可以使用super。如果没有重名,使用superthis、直接访问 效果相同。
  • 如果子类、父类和爷爷类都有同名成员,使用superthis关键字都遵循就近原则。

this和super总结

this super
意义 this是一个变量,引用当前对象的变量 仅仅是一个关键字
使用位置 不允许在静态成员中出现 不允许在静态成员中出现
使用形式 可以独立使用(是变量) 不可以独立使用(不是变量)
引用构造器 调用本类的构造器。只能在构造器首行 调用直接父类的构造器。只能在父类构造器首行
引用变量 访问本类中属性,如果本类没有则从父类继续查找。 访问父类中的属性,如果父类没有则向上继续查找。
引用方法 访问本类中方法,如果本类没有则从父类继续查找。 调用父类中的方法,如果父类没有则向上继续查找。
posted @ 2023-02-12 18:55  msuenb  阅读(5)  评论(0编辑  收藏  举报