Python | Java |
A class might be defined just for use in another class definition. For example, a LinkedStack class might use a OneWayNode class. Ideally, the definition of OneWayNode would be nested within LinkedStack, but that is not allowed. For the sake of comparison, here is an implementation of these two classes:
class OneWayNode: def __init__(self, data, next): class LinkedStack: def __init__(self): def push(self, element): def pop(self): def peek(self): def isEmpty(self): def __len__(self): |
A class might be defined just for use in another class definition. For example, a LinkedStack class might use a OneWayNode class. The definition of OneWayNode can be nested within LinkedStack. OneWayNode is specified as a private class, sometimes also called an inner class. Here is an implementation:
public class LinkedStack<E> implements TrueStack<E>{ private OneWayNode<E> items; public LinkedStack(){ public void push(E element){ public E peek(){ public boolean isEmpty(){ public int size(){ public Iterator<E> iterator(){ private class OneWayNode<E>{ private E data; private OneWayNode(E data, OneWayNode next){ |