温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Java中Super怎么调用父类方法

发布时间:2025-11-03 21:33:22 来源:亿速云 阅读:94 作者:小樊 栏目:编程语言

在Java中,要调用父类的方法,您需要使用super关键字。super关键字允许子类访问其父类的方法和构造函数。以下是使用super调用父类方法的示例:

class Parent {
    void display() {
        System.out.println("This is the Parent class method.");
    }
}

class Child extends Parent {
    void display() {
        System.out.println("This is the Child class method.");
    }

    void callParentMethod() {
        super.display(); // 调用父类的display方法
    }
}

public class Main {
    public static void main(String[] args) {
        Child child = new Child();
        child.callParentMethod(); // 输出:This is the Parent class method.
    }
}

在这个例子中,Child类继承了Parent类。Child类有一个名为display的方法,它覆盖了父类的display方法。我们创建了一个名为callParentMethod的方法,在这个方法中,我们使用super.display()调用了父类的display方法。当我们创建一个Child对象并调用callParentMethod方法时,它将输出父类的display方法的内容。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI
助
手