PP 10.3
Implement the Speaker interface described in Section 10.3, and create three classes that implement Speaker in various ways. Create a driver class whose main method instantiates some of these objects and tests their abilities.
Speaker.interface
public interface Speaker {
public void speak();
public void announce(String str);
}
Singer.java
public class Singer implements Speaker{
public void speak() {
// TODO Auto-generated method stub
System.out.println("The singer sings a song.");
}
public void announce(String str) {
// TODO Auto-generated method stub
System.out.println("The singer introduces himself."+str);
}
}
Actor.java
public class Actor implements Speaker{
public void speak() {
// TODO Auto-generated method stub
System.out.println("The actor is acting.");
}
public void announce(String str) {
// TODO Auto-generated method stub
System.out.println("The actor introduces himself."+str);
}
}
MC.java
public class MC implements Speaker{
public void speak() {
// TODO Auto-generated method stub
System.out.println("The MC moderates.");
}
public void announce(String str) {
// TODO Auto-generated method stub
System.out.println("The MC introduces himself."+str);
}
}
PP10_3.java
public class PP10_3 {
public static void main(String args[]) {
Speaker speaker;
speaker = new Singer();
speaker.speak();
speaker = new Actor();
speaker.speak();
speaker = new MC();
speaker.speak();
}
}
< Console >
The singer sings a song.
The actor is acting.
The MC moderates.
'[JAVA] 개발' 카테고리의 다른 글
Java Software Solution Ch7_PP2 (0) | 2024.05.01 |
---|---|
Java Software Soution Ch7_PP8 (0) | 2024.05.01 |
Java Software Solution Ch11_PP3 ; Exception Class (0) | 2024.04.28 |
Java Software Solution Ch11_ PP1 ; Exception Class (try-catch) (0) | 2024.04.27 |
Java Software Solution Ch7_PP8 ; Interface, Class, toString() (0) | 2024.03.17 |