[英]Java: How to call super method from inner in-place class
I have base class Foo with method spam and class Bar which overrides spam .我有基础 class Foo方法spam和 class Bar覆盖spam 。 I need to call spam of base class in method of some callback object which is defined in-place:
我需要在一些就地定义的回调 object 的方法中调用基本 class 的垃圾邮件:
public class Foo {
public void spam() {
// ...
}
}
public class Bar extends Foo {
@Override
public void spam() {
objectWhichRequireCallback(new Callback {
@Override
public void onCallback() {
super.spam();
}
});
}
}
This code is not working because super is related to Callback , not Bar class.此代码不起作用,因为super与Callback相关,而不是Bar class。 Is it possible to call super method from object defined in-place?
是否可以从就地定义的 object 调用超级方法?
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.
没有意识到方法名称是相同的。
You can create a wrapper function for this, in Bar您可以为此在 Bar 中创建一个包装器 function
public class Bar...
public void mySuperSpam(){
super.spam();
}
Try this: Bar.super.spam();
试试这个:
Bar.super.spam();
Bar.this.spam();
is compiled but it will cause infinite recursion because you call the same spam() itself.已编译但它会导致无限递归,因为您调用相同的 spam() 本身。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.