Node Inner Classes
- ListNode has no use or meaning apart from the linked list
- our existing definition opens the list to privacy leaks
- a careless application programmer could get a reference and change it
- make ListNode an inner class within the 'linked list' class
- advantages
- eliminates privacy leaks
- simplifies LinkedList
- disadvantages
- reduces flexibility
public class LinkedList inner
{
private class ListNode // inner class
{
private myObject item;
private ListNode next;
public ListNode(MyObject item, ListNode next) { ... }
}
private listNode head; ...}
- using myObject as a generic
- linked lists with generics
{
private class ListNode
{
private T item;
private ListNode next; // bad
private ListNode
- use the generic type parameter firstly with the outer class as well as the inner class
- use the generic type parameter with every declaration of the generic object.
No comments:
Post a Comment