关于java中的一种书写格式!!我经常看到有一种格式——在一个方法跟的参数括号里面,写了很到一团,一般最后都会有这样的组合“    });    ”,可能数的不清楚,下面举个例子,是设置HyperlinkListener代码的,请给我讲讲这种结构,是编程习惯么,有等价书写形式么?高手求解,本人java初学者,请别拍砖,谢了~~jEditorPane.addHyperlinkListener(new HyperlinkListener() {  public void hyperlinkUpdate(HyperlinkEvent e) {
   if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
    try {
     } 
    catch (Exception ex) {
     。
     }
   }
  }
 });

解决方案 »

  1.   

    匿名类,一种习惯,改成这样也行
    class a implements HyperlinkListener {
    public void hyperlinkUpdate(HyperlinkEvent e) {
        if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
         try {}
         catch (Exception ex) {}
        }
    }
    }
    a ainstance = new a();
    jEditorPane.addHyperlinkListener(ainstance);
      

  2.   

    我也试过改成这种形式的,不过jEditorPane.addHyperlinkListener(ainstance);我里面的参数是写的this,我记得addActionListener(ActionListener l),里面跟参赛可以写this的么,这里好像不行
      

  3.   

    写this(指当前类)的话,当前类必须实现HyperlinkListener接口而且还要重写接口函数
    也即把下面这个函数放外边
    public void hyperlinkUpdate(HyperlinkEvent e) {
    if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
    try {}
    catch (Exception ex) {}
    }
    }
    和jEditorPane.addHyperlinkListener(this);都放在this类中