Java - instanceof返回false时应为true

dra*_*ius 1 java casting class

所以基本上,我在视图中实现接口,然后将视图作为自定义事件的源传递,在3个"instanceof"调用之一中返回false.

视图:

public class NamedOffensiveStatsView extends BagVectorPanel implements INamedOffensiveStatsView {
Run Code Online (Sandbox Code Playgroud)

Event.toString():

@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append(this.getSource().getClass() + ": ");
Run Code Online (Sandbox Code Playgroud)

e.toString()打印:

class pl.drag0nius.diablo3.DPSCalc.NamedOffensiveStats.NamedOffensiveStatsView$2
Run Code Online (Sandbox Code Playgroud)

instanceof返回false:

@Override
public void eventFired(Event e) {
    logger.debug("eventFired: " + e.toString());
    if (e.getSource() instanceof INamedOffensiveStatsView) {
Run Code Online (Sandbox Code Playgroud)

此外,我无法从视图转换到它的界面.

回答:

在我朋友的帮助下,我们发现了这个问题.

代码调用事件(在视图的initComponents()内部):

    jComboBox.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (comboBoxReady) {
                logger.debug("actionPerformed");
                listener.eventFired(new Event(this, "selection", jComboBox.getSelectedIndex()));
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

应该是什么:

    jComboBox.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            if (comboBoxReady) {
                logger.debug("actionPerformed");
                listener.eventFired(new Event(NamedOffensiveStatsView.this, "selection", jComboBox.getSelectedIndex()));
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

"this"引用了嵌套类,而不是视图.

akf*_*akf 7

所述$2在所述类名的末尾表示源似乎是一个匿名内部类NamedOffensiveStatsView.因此,它不会是一个例子NamedOffensiveStatsView