用作替代接口内部类的实例 [英] Example of inner classes used as an alternative to interfaces

查看:120
本文介绍了用作替代接口内部类的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被告知,这引发了我的好奇心,关于这个主题:

What I was told, which sparked my curiosity on this topic:

Java的GUI类可以实现数百名听众和回调,很多书教你实现你的GUI类中的所有这些接口。另外,这些方面可以用内部类实现,所以方法调用的听众不会被弄混了。

Java gui classes can implement hundreds of Listeners and Callbacks and many books teach you to implement all these interfaces in your gui class. Alternatively, these aspects can be implemented in inner classes, so methods called by that listeners do not get mixed up.

我想知道如何做到这一点的动作,它不具有内部类,但私有类。但是,我不认为我完全体会到什么内部类是什么,所以我只是想换行我的头周围的情况下,我会用他们通过自己的惯例来组织一个类的方法。

I'd like to know how to do this in ActionScript, which doesn't have inner classes, but has private classes. But, I don't think I fully realize what inner classes are about, so I'm merely trying to wrap my head around the situation where I would use them to organize a class' methods by their usages.

请展示如何做到这一点看在ActionScript的例子,如果可能的话,否则的Java。

Please show an example of how this would look in ActionScript, if possible, otherwise Java.

推荐答案

在java中,它看起来像:

In java it looks like that:

  new JButton().addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent e) {
          // code that will be performed on any action on this component
      }
  };

在这里的ActionListener - 是一个接口,并通过调用新的ActionListener(){/ *接口方法实现到这里* /}; 你创建匿名类(匿名的,因为它没有名字) - 实现该接口的

here ActionListener - is an interface, and by calling new ActionListener() {/*interfaces method implementations goes here*/}; you're creating anonymous class (anonymous because it has no name) - implementation of that interface.

也可以使这样的内部类:

Or you can make inner class like this:

 class MyActionListener implements ActionListener {
   public void actionPerformed(ActionEvent e) {
      // code that will be performed on any action on this component
   }
 };

,然后用它是这样的:

and then use it like this:

 new JButton().addActionListener(new MyActionListener());

另外,你可以声明监听器作为一个顶级或静态内部类。但是,使用匿名内部类,有时是非常有用的,因为它可以让你几乎实现在哪些动作监听器监听到组件声明同一个地方你的听众。显然,这不会是一个好主意,如果监听器方法code是很长。然后,它会更好地将其移动到一个非匿名的内部或静态嵌套或顶层类。

Moreover you can declare your listener as a top-level or static inner class. But using anonymous inner class sometimes is very useful because it allows you to implement your listener almost in the same place where the component which actions your listener is listening to is declared. Obviously it won't be a good idea if the listeners methods code is very long. Then it would be better to move it into a non-anonymous inner or static nested or top-level class.

在一般情况下,肠子类是非静态类以某种方式位于所述顶层类的身体内部。在这里,你可以看到他们的Java的例子:

In general, innner classes are non-static classes that somehow resides inside the body of the top-level class. Here you can see examples of them in Java:

//File TopClass.java
class TopClass {
    class InnerClass {
    }
    static class StaticNestedClass {
    }
    interface Fooable {
    }   
    public void foo() {
        new Fooable(){}; //anonymous class
        class LocalClass { 
        }
    }
    public static void main(String... args) {
        new TopClass();
    }
}

这篇关于用作替代接口内部类的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆