在 Java 中,如何从派生类中的覆盖方法调用基类的方法? [英] In Java, how do I call a base class's method from the overriding method in a derived class?

查看:21
本文介绍了在 Java 中,如何从派生类中的覆盖方法调用基类的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Java类:B,它扩展了另一个类A,如下:

I have two Java classes: B, which extends another class A, as follows :

class A {
    public void myMethod() { /* ... */ }
}

class B extends A {
    public void myMethod() { /* Another code */ }
}

我想从 B.myMethod() 调用 A.myMethod().我来自 C++ 世界,而且我不知道如何用 Java 来做这个基本的事情.

I would like to call the A.myMethod() from B.myMethod(). I am coming from the C++ world, and I don't know how to do this basic thing in Java.

推荐答案

您要查找的关键字是 super.例如,请参阅本指南.

The keyword you're looking for is super. See this guide, for instance.

这篇关于在 Java 中,如何从派生类中的覆盖方法调用基类的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆