从嵌套匿名类中引用匿名对象
是否可以做这样的事情(我使用初始化块来缩短示例)
new A() {{
new B() {{
method(outer.this);
}}
}}
,我将外部对象的 this 作为参数提供给第二个匿名类中的方法调用?我无法使用 A.this,这会产生编译错误。
注意:给定的代码无法编译,它应该只说明我想要实现的目标。
编辑:更接近实际用例的示例:
public class Outer {
public SomeBean createBean() {
return new SomeBean() {
private final Object reference = new SomeClass() {
@Override
public void notify() {
Outer.callback(/*what goes here???*/);
}
};
//Methods...
};
}
public static void callback(final SomeBean bean) {
// do stuff with bean
}
}
我得到的编译错误只是我没有为回调提供正确的参数,因为我不知道如何引用 SomeBean 子类...
Is it possible to do something like this (I use initializer blocks to shorten the example)
new A() {{
new B() {{
method(outer.this);
}}
}}
Where I supply the this of the outer object as a parameter to the method call within the second anonymous class? I cannot use A.this, this gives a compile error.
Note: the given code does not compile, it should only illustrate what I'm trying to achieve.
Edit: example that lies closer to the actual use case:
public class Outer {
public SomeBean createBean() {
return new SomeBean() {
private final Object reference = new SomeClass() {
@Override
public void notify() {
Outer.callback(/*what goes here???*/);
}
};
//Methods...
};
}
public static void callback(final SomeBean bean) {
// do stuff with bean
}
}
And the compile error I get is just that I'm not providing the correct argument to callback, as I don't know how to reference the SomeBean subclass...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果你真的必须,我想这应该可行。
(历史注释:对于
-target 1.3
或更早版本,这应该是 NPE。)如果您不需要
A
内部类的确切类型。If you really must, I guess this should work.
(Historical note: With
-target 1.3
or earlier, this should NPE.)If you don't need the exact type of the
A
inner class.@TomHawtin 的答案很好,我的答案很相似。我会这样做:
这可能会给你带来比调用方法来获取 A 实例稍微更好的性能。主要的好处是我认为这更容易阅读/维护。编辑:
@Tomas 的答案也非常相似,但要求您在外部类中保留对新 A 对象的引用,而在外部类中可能不需要它。
根据 op 的编辑:
仅供参考 obj.notify() 是 Object 中的最终方法,您不能覆盖它。 JavaDoc 此处
@TomHawtin 's answer is good, mine is quite similar. I would do this:
This will probably give you slightly better performance than calling a method to get your A instance.The main benefit is IMO this is easier to read/maintain.Edit:
@Tomas 's answer is also very similar, but required that you keep a reference to your new A object in the outer-outer class, where it might not be needed.
In light of op's edit:
FYI obj.notify() is a final method in Object, you can't override it. JavaDoc here
考虑重构您的代码,因为您提出的代码看起来不太符合逻辑,并且肯定不容易处理或维护。
您可以使用类似这样的结构(子类化两种不同类型并使用另一种类型)来实现非常相似的结构:
如果您需要覆盖这两个类,正如您所公开的那样。也许重新思考一下可以让您摆脱这些匿名类(或者至少是其中的一些)。
Consider refactoring your code, as the code you are proposing doesn't seem too logical, and for sure not easy to handle or mantain.
You could do a pretty similar structure (subclassing two different types and using one from another) with something like this:
This, in case you need to overwrtie both classes, as you exposed. Maybe a little rethink could save you from these anonymous classes (or at least, from some of them).