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

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

问题描述

有人告诉我的话激发了我对这个话题的好奇心:

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.

我想知道如何在没有内部类但有私有类的 ActionScript 中执行此操作.但是,我认为我并没有完全理解内部类是什么,所以我只是试图围绕我会使用它们来组织类的方法的情况.

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 - 是一个接口,通过调用 new ActionListener() {/*interfaces 方法实现进入这里*/}; 你正在创建匿名类(匿名,因为它没有名字) - 该接口的实现.

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
   }
 };

然后像这样使用它:

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

此外,您可以将您的侦听器声明为顶级或静态内部类.但是使用匿名内部类有时非常有用,因为它允许您几乎在声明侦听器正在侦听的操作的组件的同一位置实现侦听器.显然,如果侦听器方法代码很长,这不是一个好主意.那么最好将其移动到非匿名内部或静态嵌套或顶级类中.

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天全站免登陆