# 리뷰: 인터페이스 String.format을 이용하여 작성해보고 싶은데 가능한가요 ?? #### CODE <a class='btn btn-default' href='/codes/98552'>Link</a> ``` import java.util.ArrayList; public class InterfaceReview { public static void main(String[] args) { // 객체 생성 Sounding d = new Dog("개"); Sounding b = new Baby("애"); Sounding t = new Tiger("호"); Sounding r = new Robot("로"); // 인터페이스 배열 생성 ArrayList<Sounding> list= new ArrayList<Sounding>(); list.add(d); list.add(b); list.add(t); list.add(r); // 소리내기 for(int i = 0; i < list.size(); i++) { Sounding e = list.get(i); String a = e.sound(); System.out.printf("%s: %s", e, a); } } } /* 인터페이스 및 클래스를 작성하시오. */ interface Sounding { public void sound(); } class Dog implements Sounding { private String name; public Dog(String name) { this.name = name; } public void sound() { return String.format("멍멍"); } } class Baby implements Sounding { private String name; public Baby(String name) { this.name = name; } public String sound() { return String.format("응애"); } } class Tiger implements Sounding { private String name; public Tiger(String name) { this.name = name; } public String sound() { return String.format("어흥"); } } class Robot implements Sounding { private String name; public Robot(String name) { this.name = name; } public String sound() { return String.format("삐빕"); } } ``` #### INPUT ``` ``` #### OUPUT ``` /root/var/tmp/2024_01_01_23_40_02_21dc44f8/InterfaceReview.java:22: error: incompatible types: void cannot be converted to String String a = e.sound(); ^ /root/var/tmp/2024_01_01_23_40_02_21dc44f8/InterfaceReview.java:41: error: incompatible types: unexpected return value return String.format("멍멍"); ^ /root/var/tmp/2024_01_01_23_40_02_21dc44f8/InterfaceReview.java:45: error: Baby is not abstract and does not override abstract method sound() in Sounding class Baby implements Sounding { ^ /root/var/tmp/2024_01_01_23_40_02_21dc44f8/InterfaceReview.java:52: error: sound() in Baby cannot implement sound() in Sounding public String sound() { ^ return type String is not compatible with void /root/var/tmp/2024_01_01_23_40_02_21dc44f8/InterfaceReview.java:57: error: Tiger is not abstract and does not override abstract method sound() in Sounding class Tiger implements Sounding { ^ /root/var/tmp/2024_01_01_23_40_02_21dc44f8/InterfaceReview.java:64: error: sound() in Tiger cannot implement sound() in Sounding public String sound() { ^ return type String is not compatible with void /root/var/tmp/2024_01_01_23_40_02_21dc44f8/InterfaceReview.java:69: error: Robot is not abstract and does not override abstract method sound() in Sounding class Robot implements Sounding { ^ /root/var/tmp/2024_01_01_23_40_02_21dc44f8/InterfaceReview.java:76: error: sound() in Robot cannot implement sound() in Sounding public String sound() { ^ return type String is not compatible with void 8 errors ```
제공된 자바 코드를 검토한 결과, 몇 가지 수정이 필요해 보입니다. 1. **`Sounding` 인터페이스의 `sound` 메서드**: `sound` 메서드가 `void` 타입으로 선언되어 있는데, `Dog`, `Baby`, `Tiger`, `Robot` 클래스에서는 `String`을 반환하고 있습니다. 이 메서드는 `String`을 반환하도록 변경해야 합니다. 2. **클래스의 `sound` 메서드**: 각 클래스에서 `sound` 메서드의 반환 타입을 `void`에서 `String`으로 변경해야 합니다. 또한 `String.format` 메서드는 현재 필요하지 않습니다. 문자열 리터럴을 직접 반환할 수 있습니다. 3. **`main` 메서드 내의 출력 부분**: 객체의 문자열 표현(`e.toString()`)과 소리(`a`)를 출력하려면, 클래스에 `toString` 메서드를 오버라이드 해야 합니다. 현재 구현에서 `e`를 출력하면 클래스 이름과 해시코드가 출력됩니다. 수정된 `Sounding` 인터페이스와 클래스는 다음과 같습니다: ```java interface Sounding { public String sound(); // 메서드 시그니처 변경 } class Dog implements Sounding { private String name; public Dog(String name) { this.name = name; } @Override public String toString() { return name; } public String sound() { return "멍멍"; // String.format 제거 } } // Baby, Tiger, Robot 클래스도 비슷한 방식으로 수정 ``` 그리고 `main` 메서드에서 출력 부분을 다음과 같이 수정할 수 있습니다: ```java for (Sounding e : list) { System.out.printf("%s: %s\n", e.toString(), e.sound()); } ``` 이러한 변경을 통해 각 객체의 이름과 해당 객체가 내는 소리를 올바르게 출력할 수 있습니다.