100_lecture27

100_lecture27 - Introduction to Computation and Problem...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Computation and Problem Solving Prof. Steven R. Lerman and Dr. V. Judson Harward Class 27: Class 27: Nested Classes and Nested Classes and an Introduction to Trees an Introduction to Trees 2 Goals To explain in more detail the different types of nested classes and how to use them. A nested class is one that is defined inside another class. To introduce the large family of data structures known as trees 1 3 static Nested Classes You can define a static nested class inside another class: public abstract class java.awt.geom.Line2D { public static class Double { ... } public static class Float { ... } } 4 static Nested Classes, 2 except that its true name is the outer class name concatenated with the inner class name: e.g., Line2D.Double the enclosing class: Make it public classes to use it Make it private in the enclosing class It behaves like any other top level class A nested class is considered to be part of if you want methods in other if you are only going to use it 2 5 private static Nested Class } public class SLinkedList implements List { private int length = 0; private SLink first = null; private SLink last = null; private static class SLink { Object item; SLink next; SLink( Object o, SLink n ) { item = o; next = n; } SLink( Object o ) { this( o, null ); } 6 Inner Classes If a nested class is not static class . using new class. variables and methods of the enclosing class instance. without arguments. , we call it an inner Instances of inner classes are usually created in an instance method of the enclosing Inner class methods have access to the instance Inner classes can have constructors with or 3 7 ListIterator as an Inner Class Example } public class SLinkedList implements List { private SLink first = null; ... public class SLinkedListIterator implements ListIterator { private SLink previous = null; private SLink current = null; private SLink next; public SLinkedListIterator( ) { next = first; current = null; ... 8 Are cheap inner classes in the sense interface. implement listeners or adapters. Anonymous Inner Classes that they are easy to define They are unnamed, so you can only create a single instance at the place you define them. They must extend a class or implement an They cant have a defined constructor and, therefore, always use a default constructor. Are commonly used in Swing code to 4 9 Listener Example { public AnonExample() { 10 Listener Example, 2 myB.addActionListener( new ActionListener() { { countLabel.setText( String.valueOf( ++count )); } } ); getContentPane().add( myPanel, } Variables from the public class AnonExample extends JFrame private JLabel countLabel; private int count = 0; JPanel myPanel = new JPanel(); JButton myB = new JButton( "Increment" ); myPanel.add( myB ); countLabel = new JLabel( 0" ); myPanel.add( countLabel ); public void actionPerformed(ActionEvent e) BorderLayout.CENTER ); enclosing class 5 11 { { b.addActionListener(this);b....
View Full Document

Page1 / 22

100_lecture27 - Introduction to Computation and Problem...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online