1 / 6

Two cool ideas: Anonymous classes Polymorphism

Two cool ideas: Anonymous classes Polymorphism. Anonymous classes – motivation. You probably have many buttons and/or menu items in your VectorGraphics project Three approaches for responding to the events from selecting those buttons / menu items

teresa
Download Presentation

Two cool ideas: Anonymous classes Polymorphism

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Two cool ideas:Anonymous classesPolymorphism

  2. Anonymous classes – motivation • You probably have many buttons and/or menu items in your VectorGraphics project • Three approaches for responding to the events from selecting those buttons / menu items • Classic: Each button is a class that implements ActionListener • Least code: Panel responds to ALL buttons • Anonymous class for each button

  3. Anonymous classes – motivation • Classic: Each button (likewise for menu-item) is a class that implements ActionListener • Obeys Quality Tip: Buttons should respond to themselves public class XXXButton extends JButton implements ActionListener { public XXXButton(XXPanel panel) { // store panel in field } public void actionPerformed(ActionEvent event) { // Ask panel to ... }

  4. Anonymous classes – motivation • Panel responds to ALL the buttons and menu-items • Not very OO, but easy to code Or this if this code is in the Panel class Wherever buttons are constructed: button.addActionListener(panel); • public class XXXPanel extends JPanel • implements ActionListener { • public void actionPerformed(ActionEvent event) { • JButton button = (JButton) (event.getSource()); • if (button.getText().equals(“Make rectangle”) { • // construct and draw a rectangle • } else if (...) { • // etc • } // etc • } • }

  5. Anonymous classes • Button responds via an anonymous class • Responding code is physically close to constructing code • Code in red below is the anonymous class Wherever buttons are constructed: button.addActionListener(new ActionListener() { public void actionPerformed(ActionEventevent) { // Ask panel to ... } }); • The anonymous class is an inner class and hence can refer to: • Any field of the enclosing class • Any local variable in the enclosing method if the variable is final.

  6. Polymorphism • You probably have a list of objects that paintComponent draws: ArrayList<MyShape> objectsToDraw; • Suppose MyShape is an interface that specifies a draw method that takes a Graphics object. Then paintComponent(Graphics g) can be: for (MyShapeobjectToDraw : objectsToDraw) { objectToDraw.draw(g); } • At run time, each objectToDrawmorphs into the particular type it actually is, and uses its actualdraw method. • Bottom-line: for any statement like x.foo(…); the actual type of x (not the declared type) is what determines which foo function to run

More Related