[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[On Teaching and Learning]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[SamR]
[Java 1.5 API]
[Espresso]
[TAO of Java]
[CS152 2004F]
[CS152 2005S]
[CS152 2005F]
Back to Reuse through Inheritance. On to Arrays and Vectors.
Held: Monday, March 6, 2006
Summary: Today we consider generics in Java. Generics are a new feature of Java 1.5 that permit us to give parameters to type (class) definitions.
Related Pages:
Due
Assignments
Notes:
Overview:
tmp = a; a = b; b = tmp;
getValue
and
setValue
methods that permit us to do something like
the following
tmp = a.getValue(); a.setValue(b.getValue()); b.setValue(tmp);
Integer
public class IntegerVariable { Integer value; public IntegerVariable(Integer _value) { this.value = _value; } // IntegerVariable(Integer) public void setValue(Integer newvalue) { this.value = newvalue; } // setValue(Integer) public Integer getValue() { return this.value; } // getValue() public void swap(IntegerVariable other) { Integer tmp = this.value; this.setValue(other.getValue()); other.setValue(tmp); } // swap(IntegerVariable other) } // class IntegerVariable
XXXVariable
class for every class XXX
public class Variable { Integer value; public Variable(Object _value) { this.value = _value; } // Variable(Object) public void setValue(Object newvalue) { this.value = newvalue; } // setValue(Object) public Object getValue() { return this.value; } // getValue() public void swap(Variable other) { Object tmp = this.value; this.setValue(other.getValue()); other.setValue(tmp); } // swap(Variable other) } // class Variable
Variable<T&*gt;
T
anywhere we want in the code.
public class Variable<T> { Integer value; public Variable(T _value) { this.value = _value; } // Variable(T) public void setValue(T newvalue) { this.value = newvalue; } // setValue(T) public T getValue() { return this.value; } // getValue() public void swap(Variable<T> other) { T tmp = this.value; this.setValue(other.getValue()); other.setValue(tmp); } // swap(Variable<T> other) } // class Variable<T>
Variable<Integer> ivar = new Variable<Integer>(new Integer(2)); Variable<String> svar = new Variable<String>("Hello");
Comparable<T>
.
Comparator<T>
.
max
.
Back to Reuse through Inheritance. On to Arrays and Vectors.
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[On Teaching and Learning]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[SamR]
[Java 1.5 API]
[Espresso]
[TAO of Java]
[CS152 2004F]
[CS152 2005S]
[CS152 2005F]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Tue May 9 08:31:40 2006.
The source to the document was last modified on Thu Jan 12 14:58:06 2006.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2006S/Outlines/outline.25.html
.
You may wish to validate this document's HTML ; ; Check with Bobby
Samuel A. Rebelsky, rebelsky@grinnell.edu