Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

PlantUML Macro
interface "Iterable<T>" as iterable {
	Iterator<T> iterator()
}
interface "Collection<T>" as collection
interface "List<T>" as list
class "ArrayList<T>" as arraylist
class "LinkedList<T>" as linkedlist

iterable <|-right-- collection
collection <|-right-- list
list <|.down. arraylist
list <|.down. linkedlist

...

Klasse som bruker ArrayListKlasse som også implementerer Iterablefor-each-løkke
Code Block
languagejava
public class Library {

	private Collection<Book> books = new ArrayList<Book>();

	public void addBook(Book book) {
		books.add(book);
	}
	public void removeBook(Book book) {
		books.remove(book);
	}
}
Code Block
languagejava
public class Library implements Iterable<Book> {

	... books-feltet og add- og remove-metodene her ...

	// fra Iterable<Book>
	public Iterator<Book> iterator() {
		return books.iterator();
	}
}
PlantUML Macro
interface "Iterable<T>" as iterable {
	Iterator<T> iterator()
}
class Library

iterable <|.right.. Library
Code Block
// lag en Library-instans
Library library = new Library();
// legg til noen bøker
library.addBook(new Book(...));
library.addBook(new Book(...));

// gå gjennom bøkene
for (Book book : library) {
	// gjør noe med book her
	...
}

...