Gerarchie e polimorfismo liste 1 Generalizzare le liste

  • Slides: 8
Download presentation
Gerarchie e polimorfismo: liste 1

Gerarchie e polimorfismo: liste 1

Generalizzare le liste di interi 4 List 4 lista di oggetti – non modificabile

Generalizzare le liste di interi 4 List 4 lista di oggetti – non modificabile 4 vorremo poi definire un sottotipo – versione ordinata 2

List 4 classe astratta 4 usate i sottotipi per implementare i due casi della

List 4 classe astratta 4 usate i sottotipi per implementare i due casi della definizione ricorsiva – lista vuota – lista non vuota 3

Specifica del supertipo List public abstract class List { // OVERVIEW: un List è

Specifica del supertipo List public abstract class List { // OVERVIEW: un List è una lista non modificabile di Objects. // Elemento tipico [x 1, . . . , xn] public abstract Object first () throws Empty. Exception; // EFFECTS: se this è vuoto solleva Empty. Exception, altrimenti // ritorna il primo elemento di this public abstract List rest () throws Empty. Exception; // EFFECTS: se this è vuoto solleva Empty. Exception, altrimenti // ritorna la lista ottenuta da this togliendo il primo elemento public abstract Iterator elements (); // EFFECTS: ritorna un generatore che produrrà tutti gli elementi di // this (come Objects) nell’ordine che hanno in this public abstract List add. El (Object x); // EFFECTS: restituisce la lista ottenuta aggiungendo x all’inizio di this public abstract List rem. El (Object x); // EFFECTS: restituisce la lista ottenuta rimuovendo x da this public abstract int size (); // EFFECTS: ritorna il numero di elementi di this public abstract boolean rep. Ok (); public String to. String (); public boolean equals (List o); } 4

Implementazione del supertipo List public abstract class List { // OVERVIEW: un List è

Implementazione del supertipo List public abstract class List { // OVERVIEW: un List è una lista non modificabile di Objects. // Elemento tipico [x 1, . . . , xn] // metodi astratti public public abstract abstract // metodi concreti Object first () throws Empty. Exception; List rest () throws Empty. Exception; Iterator elements (); List add. El (Object x); List rem. El (Object x); int size (); boolean rep. Ok (); public String to. String () {. . } public boolean equals (List o) {. . } } 4 implementare to. String e equals – utilizzando il generatore elements 5

Implementazione del sottotipo Empty. List public class Empty. List extends List { public Empty.

Implementazione del sottotipo Empty. List public class Empty. List extends List { public Empty. List () {} public Integer first () throws Empty. Exception {. . } public List rest () throws Empty. Exception {. . } public Iterator elements () { return new Empty. Gen(); } public List add. El (Object x) {. . } public List rem. El (Object x) {. . } public int size () {. . } public boolean rep. Ok () {. . } static private class Empty. Gen implements Iterator { Empty. Gen () {} public boolean has. Next () { return false; } public Object next () throws No. Such. Element. Exception { throw new No. Such. Element. Exception("List. elements"); } } } 6

Implementazione del sottotipo Full. List public class Full. List extends List { private int

Implementazione del sottotipo Full. List public class Full. List extends List { private int sz; private Object val; private List next; public Full. List (Object x) {sz = 1; val = x; next = new Empty. List ( ); } public Integer first () throws Empty. Exception {. . } public List rest () throws Empty. Exception {. . } public Iterator elements () {. . } public List add. El (Object x) {. . } public List rem. El (Object x) {. . } public int size () {. . } public boolean rep. Ok () {. . }} 7

Il sottotipo Ordered. List public class Ordered. List extends? List { // OVERVIEW: una

Il sottotipo Ordered. List public class Ordered. List extends? List { // OVERVIEW: una Ordered. List è un sottotipo di List, che ha una operazione in più // per inserire un elemento che tiene conto dell’ordine public Ordered. List (); // EFFECTS: restituisce la lista ottenuta inserendo x in this public Ordered. List add. El (Comparable x); } // EFFECTS: restituisce la lista ottenuta inserendo x in this in modo che il // risultato sia una lista ordinata. Solleva leveccezioni che deve 4 rifarlo anche con sottotipi di Comparator 8