从嵌套匿名类中引用匿名对象

发布于 2024-12-23 16:36:08 字数 699 浏览 4 评论 0原文

是否可以做这样的事情(我使用初始化块来缩短示例)

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

如何视而不见 2024-12-30 16:36:08

如果你真的必须,我想这应该可行。

new A() {
    {
        new B() {{
            method();
        }};
    }
    private void method() {
        method(this);
    }
}

(历史注释:对于 -target 1.3 或更早版本,这应该是 NPE。)

如果您不需要 A 内部类的确切类型。

new A() {
    {
        new B() {{
            method(a);
        }};
    }
    private A a() {
        return this;
    }
}

If you really must, I guess this should work.

new A() {
    {
        new B() {{
            method();
        }};
    }
    private void method() {
        method(this);
    }
}

(Historical note: With -target 1.3 or earlier, this should NPE.)

If you don't need the exact type of the A inner class.

new A() {
    {
        new B() {{
            method(a);
        }};
    }
    private A a() {
        return this;
    }
}
静待花开 2024-12-30 16:36:08

@TomHawtin 的答案很好,我的答案很相似。我会这样做:

new A() {
    private final A anon = this;
    /*init*/ {
        new B() {
            /*init*/ {
                method(anon);
            }
        }
    }
}

这可能会给你带来比调用方法来获取 A 实例稍微更好的性能。主要的好处是我认为这更容易阅读/维护。

编辑:
@Tomas 的答案也非常相似,但要求您在外部类中保留对新 A 对象的引用,而在外部类中可能不需要它。

根据 op 的编辑:

public SomeBean createBean() {
    SomeBean myBean = new SomeBean() {
        private final Object reference = new SomeClass() {
            @Override
            public void notify() {
                Outer.callback(/*what goes here???*/);
            }
        };
        //Methods...
    };
    return myBean;
}

仅供参考 obj.notify() 是 Object 中的最终方法,您不能覆盖它。 JavaDoc 此处

@TomHawtin 's answer is good, mine is quite similar. I would do this:

new A() {
    private final A anon = this;
    /*init*/ {
        new B() {
            /*init*/ {
                method(anon);
            }
        }
    }
}

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:

public SomeBean createBean() {
    SomeBean myBean = new SomeBean() {
        private final Object reference = new SomeClass() {
            @Override
            public void notify() {
                Outer.callback(/*what goes here???*/);
            }
        };
        //Methods...
    };
    return myBean;
}

FYI obj.notify() is a final method in Object, you can't override it. JavaDoc here

阳光下的泡沫是彩色的 2024-12-30 16:36:08

考虑重构您的代码,因为您提出的代码看起来不太符合逻辑,并且肯定不容易处理或维护。
您可以使用类似这样的结构(子类化两种不同类型并使用另一种类型)来实现非常相似的结构:

//declaring a variable as final enables accessing it from the B subtype if declared at the same scope (method)
final A myA=new A() {
   //personalized content for type A
};
//then, declare the B subtype, using the final myA var as you need, at the constructor 
B myB=new B() {{
   method(myA);
}};

如果您需要覆盖这两个类,正如您所公开的那样。也许重新思考一下可以让您摆脱这些匿名类(或者至少是其中的一些)。

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:

//declaring a variable as final enables accessing it from the B subtype if declared at the same scope (method)
final A myA=new A() {
   //personalized content for type A
};
//then, declare the B subtype, using the final myA var as you need, at the constructor 
B myB=new B() {{
   method(myA);
}};

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文