You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java继承中this关键字调用方法疑问:为何触发子类重写方法?

Java继承中方法调用与成员变量访问的差异解析

先看你给出的代码:

class A {
    int n;
    public A() {
        this.n = 1;
    }
    public void compute() {
        this.n = this.n * 2;
    }
    public void print() {
        this.compute();
        System.out.println("A: " + this.n);
    }

    public static void main(String[] args) {
       B b = new B();
       b.print();
    }
}

class B extends A {
    int n;
    public B() {
        this.n = 5;
    }
    public void compute() {
        this.n = this.n * 2;
    }
    public void print() {
       super.print();
       System.out.println("B: " + this.n);
    }
}

这个现象的核心原因是Java中方法调用的动态绑定,和成员变量访问的静态绑定规则不同,具体拆解如下:

1. 方法调用:动态绑定(运行时多态)

Java中,所有非静态的成员方法调用,都是基于对象的实际类型来确定的,而非引用的声明类型。

  • 当b.print()执行时,进入B类的print方法,调用super.print()进入A类的print方法。
  • 在A类的print里执行this.compute(),这里的this指向的是实际创建的B类对象(而非A类对象),所以会调用B类中重写的compute方法,把B类自己的n从5改成10。

2. 成员变量访问:静态绑定(编译时确定)

成员变量不存在“重写”的概念,子类的同名变量只是隐藏了父类的变量,两者是独立的存储空间。

  • A类的print方法里的this.n,在编译阶段就已经确定是A类定义的n——因为这段代码写在A类中,编译器只会识别当前类的成员变量。
  • 所以即使this实际指向B对象,这里打印的依然是A类的n(值为1,由A的构造函数初始化后未被修改过)。

完整执行流程验证

  1. 创建B对象时,先执行父类A的构造函数,A的n被设为1;再执行B的构造函数,B的n被设为5。
  2. 调用b.print()进入B的print,执行super.print()进入A的print。
  3. A的print调用this.compute(),实际执行B的compute,B的n变为10。
  4. A的print打印A: 1(A类的n始终是1)。
  5. 回到B的print,打印B: 10(B类的n已经被修改为10)。

内容的提问来源于stack exchange,提问作者231_koh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 09:02:16