简体   繁体   English

Java-如何从参数访问外部类?

[英]Java - How to access outer class from argument?

public class Outer {
    public void Say(Bar bar) {
        //...
    }
    public Outer(final Foo foo) {
        //...
    }
}

public interface Foo {
    void blah(Bar bar);
}

public class Application {
    public static void main(String[] args) {
        Outer outer = new Outer(new Foo() { 
            @Override
            public void blah(Bar bar) {
                /*trying to access "outer"*/.Say(bar);
            }
        });
    }
}



What should I do to access "outer"? 我应该怎么做才能访问“外部”?
Thanks. 谢谢。



StackOverflow forbade me from posting this because it said my post was mainly code and this line is just a bunch of nonsense. StackOverflow禁止我发布此内容,因为它说我的发布内容主要是代码,而这一行只是一堆废话。 Please ignore it. 请忽略它。

Outer is not reachable. 外部无法到达。 As the instance is created after Foo is created. 在创建Foo之后创建实例。

I see two ways: 1. Convert the variable outer into a static context. 我看到两种方法:1.将变量external转换为静态上下文。 2. Hand over the variable while calling method blah() 2.调用方法blah()时移交变量

在您的示例中,“外部”就是“这个”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM