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