Java:如何从内部就位类调用超级方法 [英] Java: How to call super method from inner in-place class
本文介绍了Java:如何从内部就位类调用超级方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有带有方法 spam 的基类 Foo 和覆盖 spam 的类 Bar .我需要在就地定义的某些回调对象的方法中调用基类的 spam :
I have base class Foo with method spam and class Bar which overrides spam. I need to call spam of base class in method of some callback object which is defined in-place:
public class Foo {
public void spam() {
// ...
}
}
public class Bar extends Foo {
@Override
public void spam() {
objectWhichRequireCallback(new Callback {
@Override
public void onCallback() {
super.spam();
}
});
}
}
此代码不起作用,因为 super 与 Callback 相关,而不与 Bar 类相关.可以从就地定义的对象中调用超级方法吗?
This code is not working because super is related to Callback, not Bar class. Is it possible to call super method from object defined in-place?
推荐答案
public class Bar extends Foo {
@Override
public void spam() {
objectWhichRequireCallback(new Callback {
@Override
public void onCallback() {
Bar.super.spam();
}
});
}
}
编辑:抱歉.没有意识到方法名称是相同的.
EDIT : Sorry. DIdn't realize the method names are the same.
这篇关于Java:如何从内部就位类调用超级方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文