一道java考试试题求解
A customer visits a restaurant and asks for service via a bell.
The waiter receives the bell ring and answers.
The Waiter class implements the Bell interface,
and the Customer class contains a method called served(),
representing that he is served by the waiter.
served() calls Bell's answer().
Please complete the codes.
For example:
input: 10
output:
Waiter 10 receives a bell ring.
the customer is served.
只能在shart 和 end之间添加代码。
源代码:
import java.util.Scanner;
interface Bell {
void answer();
}
/******start******/
/******end******/
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int no = scanner.nextInt();
Bell bell=new Waiter(no);
Customer customer=new Customer(bell);
customer.served();
scanner.close();
}
}
完整的参考
/******start******/
class Waiter implements Bell {
public Waiter(int n) {
System.out.printf("Waiter %d receives a bell ring.
",n);
}
public void answer() {
System.out.printf("the customer is served.");
}
}
class Customer {
Bell bell;
public Customer(Bell b) {
bell=b;
}
public void served() {
bell.answer();
}
}
/******end******/