An example of inner classes used as an alternative to interfaces

I was told what aroused my curiosity on this topic:

Gui Java classes can implement hundreds of listeners and callbacks, and many books teach you how to implement all of these interfaces in your gui class. Alternatively, these aspects can be implemented in inner classes, so the methods invoked by these listeners do not mix.

I would like to know how to do this in ActionScript, which does not have inner classes but has private classes. But I don’t think I fully understand what inner classes are, so I'm just trying to circle my head around the situation when I use them to organize class methods according to their customs.

Please provide an example of how this will look in ActionScript, if possible, otherwise Java.

+8
java oop interface actionscript-3 inner-classes
source share
2 answers

In java, it looks like this:

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

here the ActionListener is the interface, and by calling new ActionListener() {/*interfaces method implementations goes here*/}; , you create an anonymous class (anonymous, because it does not have a name) - an implementation of this interface.

Or you can make an inner class as follows:

  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()); 

In addition, you can declare your listener as a top or static inner class. But using an anonymous inner class is sometimes very useful, because it allows you to implement your listener almost in the same place where the component that declares listening to your listener is declared. Obviously, it would not be a good idea if the code of the listener methods is very long. Then it would be better to move it to a non-anonymous internal or static nested or top level.

In general, inner classes are non-static classes that somehow reside inside the body of a 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(); } } 
+5
source share

Gasan provides a great example of how inner classes are commonly used for callbacks in the Java GUI. But in AS3, you usually didn’t do it like that, because AS3 event listeners are function references, not interfaces. In this regard, AS3 has more in common with JavaScript than Java.

What you can do in AS3 (as with JavaScript) instead of anonymous inner class callbacks is to create function closures.

EDIT: I found a link here that prints me very much:

ActionScript 3.0 using closures for event handlers

+2
source share

All Articles