deliverMessage():
public interface Postman {
void deliverMessage(String msg);
}
public class PostmanImpl implements Postman {
PRivate PrintStream output;
public PostmanImpl() {
output = System.out;
}
public void deliverMessage(String msg) {
output.println(" Postman " + msg);
output.flush();
}
}
public class PostmanApp {
public static void main(String[] args) throws Exception {
BufferedReader sysin = new BufferedReader(new InputStreamReader(System.in));
// OBTain a Postman instance
Postman postman = getPostman();
while (true) {
System.out.print("Enter a message: ");
String msg = sysin.readLine();
postman.deliverMessage(msg);
}
}
private static Postman getPostman() {
// Omit for now, will come back later
}
}
// MODIFIED VERSION
public class PostmanImpl implements Postman {
private PrintStream output;
// Start of modification
public PostmanImpl() throws IOException {
output = new PrintStream(new FileOutputStream("msg.txt"));
}
// End of modification
public void deliverMessage(String msg) {
output.println(" Postman " + msg);
output.flush();
}
}
新闻热点
疑难解答