본문 바로가기

[JAVA] 개발

Java Software Solution Ch10_PP3 ; Interface

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.