简体   繁体   中英

Java - As Method or Inner Class or Class?

So I've stumbled across several ways of implementing an ActionListener and I'm wondering if someone can walk me through the differences of how each works and whether there are reasons or advantages to use one over the other?

The first is below in a block of code:

public void actionPerformed(ActionEvent arg0) {
// CODE HERE
}

The second way I saw was within another block of code as:

private class myListener implements ActionListener {
// CODE HERE
}

The third was is simply having a separate class for the ActionListener, with similar code to that above, but within a separate class.

I'm wondering whether the method approach is more efficient as new objects don't have to be created for each, you simply reference this as the ActionListener rather than, for example, referencing new myListener() . Thank you.

There's no difference in speed in any of the options; you'll always have an object that implements the ActionListener interface. Avoiding an instance of a separate class will just save you a few bytes of memory.

Your choice really should be based on what makes sense for your code, structurally. For example, having your public class implement ActionListener may look weird for those who are using that class, especially if the ActionListener behavior is supposed to be private to the class and not used outside it.

So it's mostly a choice of what you think looks better in your code; the only real difference will be with regards to field / method access (eg a separate, non-inner class won't have access to private methods and fields of your class, an anonymous inner class can't access non-final variables of the enclosing method, etc).

I don't like or use "implements ActionListener".

I do like and use anonymous inner classes like:

    btnPurplescreen.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Color baseColor = Color.BLUE;
            panelImage.setBackground(baseColor);
            panelReference.setBackground(baseColor);
            panelReference2.setBackground(baseColor);
            baseType = BaseType.PURPLE;
        }
    });

You're stumbling in more ways that one.

In some sense, there is only one way to create a listener: there must be an object of a class that implements ActionListener , which means the class has the actionPerformed method.

There are three ways to do this:

You can modify a class you are already using for something else by marking it as implementing ActionListener and adding the actionPerformed method. This saves you creating a new class -- a savings of negligible value in most cases -- but mars otherwise perfectly good code. A few cases, when the existing

You can create a new named class. This is useful if you think the name is going to be meaningful to someone. If you are really using names like "MyListener", that's a clue that no, no-one cares about the name.

Finally, and usually, you can create an unnamed class. If all you want to do is add a fragment of code as a listener.

Whatever your choice, it's extremely unlikely to have any detectably effect on the time or memory performance of your finished system. The choice should be dictated by concerns about readability and maintainability.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM