Debug时含有的子元素,在代码里获取不到的问题

  比如,Debug时如下图展示:

  我想要获取的是:ansList.get(i).getComponent().getConnectorId()
  debug时明明有这个元素,但是当我写出来的时候却发现:ansList.get(88)没有getComponent()这个函数。

  这不是因为debug出错了,而是因为这里默认进行了类型转换,将ansList.get(88)转换成了WebButton类型。

  所以应当按照如下写法:

 1 ans.getComponent().addListener(event -> {
 2     String btnId = ((ClickEvent) event).getButton().getConnectorId();
 3     List<Component> ansList = (ArrayList<Component>)editGrid.getComponents();
 4     for(int i=ansList.size()-1; i>=0; i--){
 5         if(ansList.get(i)!=null && ansList.get(i) instanceof WebButton){
 6             WebButton tmpBtn = (WebButton) ansList.get(i);
 7             if(tmpBtn.getComponent().getConnectorId().equals(btnId)){
 8                //....
 9             }
10         }
11     }
12 });

  注: 父类强转成子类的时候,一定要先判断类型

原文地址:https://www.cnblogs.com/acm-bingzi/p/javaSubelement.html