[Bluej-discuss] Excercise 4.25 review please
David J. Barnes
d.j.barnes at kent.ac.uk
Mon Jun 19 09:09:48 BST 2006
JD,
Chaining together method calls like that is a relatively rare occurrence, but the possibility is worth knowing about. When explaining it to students it is probably worth breaking it down into stages.
Instead of writing:
highestBid.getBidder ().getName();
we can make use of an additional local variable:
Person winner = highestBid.getBidder();
and then make the call:
winner.getName()
Because the getBidder call returns an object reference, there is nothing to stop us in Java making the call directly on the return value. When a local variable is only being used as a staging post in this way it is sometimes omitted.
Hope that helps,
David
On Sun, 18 Jun 2006 16:00:20 -0700
JD <dnaltrop at gmail.com> wrote:
> Hi Rob -
>
> Thanks for the reply. In the end I I knew I was making an external method
> call...It just that I didnt know you could call a local object reference
> highestBid that calls another object reference getBidder() in an external
> class Bid that calls another object reference bidder in another external
> class Person that FINALLY calls a real method getName that returns the
> bidders person name....whew!.... I hope this book explores more examples of
> these chained external method calls as I am sure they are common to use.
>
> On 6/18/06, RDP <robdepasquale at msn.com> wrote:
> >
> > JD,
> >
> > You've just discovered the "eternal method call" using DOT NOTATION
> > (contrast "internal method call").
> >
> > See Page 67 in the text book which explains calling methods of other
> > objects.
> >
> > details += " sold for $" + highestBid.getValue() + " to: " +
> > highestBid.getBidder ().getName();
> >
> > Your code above is what did it.
> >
> > Unfortunately, I think the text does not explain nor present more examples
> > of external method calls using dot notation.
> >
> > Hey good luck!!
> >
> > Best regards,
> >
> > Rob DePasquale
> > Bon Vivant!
> >
> > ----- Original Message -----
> > *From:* JD <dnaltrop at gmail.com>
> > *To:* bluej-discuss at bluej.org
> > *Sent:* Sunday, June 18, 2006 1:36 PM
> > *Subject:* [Bluej-discuss] Excercise 4.25 review please
> >
> > Hi all!
> >
> > Please review and comment on my solution for Exercise 4.24. I have a
> > question at the bottom of this email as well.
> >
> > Auction Class:
> >
> > /**
> > * Close Lot
> > * @return Close lots
> > */
> >
> > public void closeAuction()
> > {
> > Iterator it = lots.iterator();
> > while(it.hasNext()) {
> > Lot lot = (Lot) it.next();
> > System.out.println(lot.closeLot());
> > }
> > }
> >
> >
> > Lot Class:
> >
> > /**
> > * @return A string representation of this lot's details.
> > */
> > public String closeLot()
> > {
> > String details = number + ": " + description;
> > if(highestBid != null) {
> > details += " sold for $" + highestBid.getValue() + " to: "
> > + highestBid.getBidder ().getName();
> > }
> > else {
> > details += " (No bid)";
> > }
> > return details;
> > }
> >
> >
> > Output:
> >
> > 1: Car sold for $3432 to: Carl
> >
> > 2: Cat (No bid)
> >
> >
> > Question:
> >
> > The following line of code gave me the most grief until I got it working.
> >
> > highestBid.getBidder().getName();
> >
> > It is an object reference to a method to another object to a method within
> > the object referenced by the getBidder() method!?!?!? It works. But what
> > the heck did I create here? What is it called? Have we covered this type
> > of construction in the book yet and I missed it?
> >
> > ------------------------------
> >
> > _______________________________________________
> > mailing list bluej-discuss at bluej.org
> > To unsubscribe or change your preferences, go to
> > http://lists.bluej.org/mailman/listinfo/bluej-discuss
> >
> >
> > _______________________________________________
> > mailing list bluej-discuss at bluej.org
> > To unsubscribe or change your preferences, go to
> > http://lists.bluej.org/mailman/listinfo/bluej-discuss
> >
> >
> >
More information about the bluej-discuss
mailing list