... name for a specific Card
. The constructor for the class (lines 9–12) receives two Strings
that it uses to initialize face
and suit
. Method toString
(lines 15–17) creates a String
consisting of the face
of the card
, the String “ of ”
and the suit
of the card.2 Card’s toString
method can be invoked explicitly to obtain a string representation of a Card
object (e.g., “Ace of Spades”
). The toString
method of an object is called implicitly when the object is used where a String
is expected (e.g., when printf
outputs the object as a String
using the %s
format specifier or when the object is concatenated to a String
using the +
operator). For this behavior to occur, toString
must be declared with the header shown in Fig. 7.11.
Get Java How To Program, Late Objects, 11th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.