Java Reference
In-Depth Information
Examining the mail system project, we see that:
It has three classes: MailServer , MailClient , and MailItem .
One mail-server object must be created that is used by all mail clients. It handles the ex-
change of messages.
Several mail-client objects can be created. Every mail client has an associated user name.
Mail items can be sent from one mail client to another via a method in the mail-client class.
Mail items can be received by a mail client from the server one at a time, using a method in
the mail client.
The MailItem class is never explicitly instantiated by the user. It is used internally in the
mail clients and server to create, store, and exchange messages.
Exercise 3.34 Draw an object diagram of the situation you have after creating a mail server
and three mail clients. Object diagrams were discussed in Section 3.6.
The three classes have different degrees of complexity. MailItem is fairly trivial. We shall
discuss only one small detail and leave the rest up to the reader to investigate. MailServer is
quite complex at this stage; it makes use of concepts discussed only much later in this topic. We
shall not investigate that class in detail here. Instead, we just trust that it does its job—another
example of the way abstraction is used to hide detail that we do not need to be aware of.
The MailClient class is the most interesting, and we shall examine it in some detail.
3.12.2 The this keyword
The only section we will discuss from the MailItem class is the constructor. It uses a Java
construct that we have not encountered before. The source code is shown in Code 3.5.
Code 3.5
Fields and constructor
of the MailItem class
public class MailItem
{
// The sender of the item.
private String from;
// The intended recipient.
private String to;
// The text of the message.
private String message;
/**
* Create a mail item from sender to the given recipient,
* containing the given message.
* @param from The sender of this item.
* @param to The intended recipient of this item.
* @param message The text of the message to be sent.
*/
 
Search WWH ::




Custom Search