Java 继承 - 调用超类方法 [英] Java Inheritance - calling superclass method

查看:38
本文介绍了Java 继承 - 调用超类方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有以下两个类

public class alpha {

    public alpha(){
        //some logic
    }

    public void alphaMethod1(){
        //some logic
    }
}

public class beta extends alpha {

    public beta(){
        //some logic
    }

    public void alphaMethod1(){
        //some logic
    }
}

public class Test extends beta
{
     public static void main(String[] args)
      {
        beta obj = new beta();
        obj.alphaMethod1();// Here I want to call the method from class alpha.
       }
}

如果我启动一个新的 beta 类型对象,我如何执行在类 alpha 中找到的 alphamethod1 逻辑而不是 beta?我可以只使用 super().alphaMethod1() <- 我想知道这是否可能.

If I initiate a new object of type beta, how can I execute the alphamethod1 logic found in class alpha rather than beta? Can I just use super().alphaMethod1() <- I wonder if this is possible.

Eclipse IDE 中的自动键入让我可以选择从 alpha 类或 beta 类中选择 alphamethod1.

Autotype in Eclipse IDE is giving me the option to select alphamethod1 either from class alpha or class beta.

推荐答案

您可以:

super.alphaMethod1();

注意,super 是对父类的引用,而 super() 是它的构造函数.

Note, that super is a reference to the parent class, but super() is its constructor.

这篇关于Java 继承 - 调用超类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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