10.12.2012 Views

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

The Java Language Specification, Third Edition

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

8.1.2 Generic Classes and Type Parameters CLASSES<br />

180<br />

T convert();<br />

}<br />

class ReprChange {<br />

T t;<br />

void set(S s) { t = s.convert(); }<br />

S get() { return t.convert(); }<br />

}<br />

Parameterized class declarations can be nested inside other declarations.<br />

DISCUSSION<br />

This is illustrated in the following example:<br />

class Seq {<br />

T head;<br />

Seq tail;<br />

Seq() { this(null, null); }<br />

boolean isEmpty() { return tail == null; }<br />

Seq(T head, Seq tail) { this.head = head; this.tail = tail; }<br />

class Zipper {<br />

Seq zip(Seq that) {<br />

if (this.isEmpty() || that.isEmpty())<br />

return new Seq();<br />

else<br />

return new Seq(<br />

new Pair(this.head, that.head),<br />

this.tail.zip(that.tail));<br />

}<br />

}<br />

}<br />

class Pair {<br />

T fst; S Snd;<br />

Pair(T f, S s) {fst = f; snd = s;}<br />

}<br />

DRAFT<br />

class Client {<br />

{<br />

Seq strs =<br />

new Seq("a", new Seq("b",<br />

new Seq()));<br />

Seq nums =<br />

new Seq(new Integer(1),<br />

new Seq(new Double(1.5),

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!