开发者

Strange Java code: Class in a Class

开发者 https://www.devze.com 2022-12-27 07:16 出处:网络
In some sample codes there are methods and classes declared WITHIN other methods and/or classes. I\'ve never heard/read about this. What effect does this kind of programming have? Wouldn\'t it be bet

In some sample codes there are methods and classes declared WITHIN other methods and/or classes.

I've never heard/read about this. What effect does this kind of programming have? Wouldn't it be better to write down classes in a seperate file and methods side by side and not within each other (like every book tells you)? What are the advantages and disadvantages of this kind of programming?

Here's an example of what I mean:

Handler mHandler = new Handler() {
    public void handleMessage(android.os.Message msg) {
        TextView textView = (TextView) findViewById(R.id.description);
        textView.setText(mRoad.mName + " " + mRoad.mDescription);
        MapOverlay m开发者_如何转开发apOverlay = new MapOverlay(mRoad, mapView);
        List<Overlay> listOfOverlays = mapView.getOverlays();
        listOfOverlays.clear();
        listOfOverlays.add(mapOverlay);
        mapView.invalidate();
    }
};


There are two types of classes that can be in a class: Inner Classes and Anonymous Classes.

Both are mainly used to create classes that do not work alone but need access to the surrounding object. These classes have full access to the whole surrounding object (exception: A definition of a static inner class).


That is called anonymous inner class. Suppose you will use the class only in a single place and the class is short and simple, then there may be no point to create a public class.

This page contains more information about anonymous inner classes.


Here is a good read on anonymous classes and why they can be useful. It is mostly used in scenarios where the implementation of the class is quite short and not re-used anywhere else. By using this anonymous implementation you can keep the code short and concise.


Anonymous classes are common when defining a small implementation of an interface you are working with, such as EventListeners. One thing to note about them is that you will NOT have access to local variables (and parameters) unless they are declared final.


Yeah, this is called: anonymous class.

You can read more about it here: http://docstore.mik.ua/orelly/java-ent/jnut/ch03_12.htm


The inner classes also have the advantage of being able to reference the container class without having an explicit pointer.

Being anonymous is just a matter of conciseness.


One of commons example is Listener implementation:

class myPanel extends JPanel {
    public MyPanel() {
        JButton b1 = new JButton("Hello");
        b1.addActionListener(
            new ActionListener() {
                public void actionPerformed(ActionEvent e) {
                    // do something for button b1
                }
            }
        );
    }
}

Now imagine that the Listener implementation can depends on some outer class (myPanel) properties. Using anonymous class You can easily do this.

0

精彩评论

暂无评论...
验证码 换一张
取 消