如何理解一个内部对象是instanceof outer class?

How to understand that an inner object is instanceof outer class?

我想看看 innerObj 是不是一种 instanceof outerClass

在示例中 al instanceof F 将 return 为假,但我想看看 al 对象是否是 F class[=16 的一部分=]

示例:

public class TestClass
{


    public static void main(String[] args)
    {
        F f = new F();
        ActionListener al = f.getAL();
        System.out.println(al.getClass()); //prints class F. Close, but it's a String..
        if (al instanceof F) {
            //false, but I want to understand that "al" was created in "F"
        }
    }
}

class F {
    ActionListener al;
    
    F(){
        al = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            }
        };
    }
    
    public ActionListener getAL() {
        return al;
    }
}

你快到了。您正在寻找 al.getClass().getEnclosingClass().

(也就是说,你要做这个检查很奇怪,而且看起来很脆弱......)