Java继承 - 调用超类方法 [英] Java Inheritance - calling superclass method
本文介绍了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而不是beta中找到的 alphamethod1
逻辑吗?我可以使用 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.
Autotype在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, but super() is it's constructor.
这篇关于Java继承 - 调用超类方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文