Java Reference
In-Depth Information
Display 14.6
Using Our Ordered Pair Class (part 2 of 2 )
19 }
20 else
21 {
22 System.out.println("You guessed incorrectly.");
23 System.out.println("You guessed");
24 System.out.println(inputPair);
25 System.out.println("The secret words are");
26 System.out.println(secretPair);
27 }
28 }
29 }
Sample Dialogue
Enter two words:
two words
You guessed incorrectly.
You guessed
first: two
second: words
The secret words are
first: Happy
second: Day
PITFALL: A Generic Constructor Name Has No Type Parameter
The class name in a parameterized class definition has a type parameter attached, such
as Pair<T> in Display 14.5. This can mislead you into thinking you need to use the
type parameter in the heading of the constructor definition, but you do not repeat
the type parameter specification <T> in the heading of the constructor definition. For
example, use
public Pair()
Do not use
public Pair<T>()
A constructor can use the type parameter, such as T , as the type for a parameter for
the constructor, as in the following, but the constructor heading does not include the
type parameter in angular brackets, such as <T> :
public Pair(T firstItem, T secondItem)
For a complete example, see Display 14.5 .
Sometimes it seems that people stay up late at night thinking of ways to make
things confusing. As we just noted, in the defi nition of a parameterized class, a
 
 
Search WWH ::




Custom Search