1. 제네릭에 대한 설명으로 틀린 것은 무엇입니까? 4
1) 컴파일시 강한 타입 체크를 할 수 있다.
2) 타입 변환(casting)을 제거한다.
3) 제네릭 타입은 타입 파라미터를 가지는 제네릭 클래스와 인터페이스를 말한다.
4) 제네릭 메소드는 리턴 타입으로 타입 파라미터를 가질 수 없다.
2. ContainerExample 클래스의 main() 메소드는 Container 제네릭 타입을 사용하고 있습니다. main() 메소드에서 사용하는 방법을 참고해서 Container 제네릭 타입을 선언해보세요.
public class ContainerExample {
public static void main(String[] args) {
Container<String> stringContainer = new Container<>();
stringContainer.set("홍길동");
String str = stringContainer.get();
Container<Integer> intContainer = new Container<>();
intContainer.set(6);
int value = intContainer.get();
}
}
<정답>
public class Container<T> {
private T name;
void set(T name) {
this.name = name;
}
T get(){
return name;
}
}
3. TwoContainerExample 클래스의 main() 메소드는 TwoContainer 제네릭 타입을 사용하고 있습니다. main() 메소드에서 사용하는 방법을 참고해서 TwoContainer 제네릭 타입을 선언해보세요.
public class TwoContainerExample {
public static void main(String[] args) {
TwoContainer<String, String> container = new TwoContainer<>();
container.set("홍길동", "도적");
String name = container.getKey();
String job = container.getValue();
TwoContainer<String, Integer> secondContainer = new TwoContainer<>();
secondContainer.set("홍길동", 35);
String name2 = secondContainer.getKey();
Integer age = secondContainer.getValue();
}
}
<정답>
public class TwoContainer<T, M> {
private T key;
private M value;
void set(T key, M value) {
this.key = key;
this.value = value;
}
public T getKey(){
return key;
}
public M getValue(){
return value;
}
4. Util.getValue() 메소드는 첫 번째 매개값으로 Pair 타입과 하위 타입만 받고, 두 번째 매개값으로 키를 받습니다. 리턴값은 키값이 일치할 경우 Pair에 저장된 값을 리턴하고, 일치하지 않으면 null을 리턴하도록 getValue() 제네릭 메소드를 정의해보세요.
public class UtilExample {
public static void main(String[] args) {
Pair<String, Integer> pair = new Pair<>("홍길동", 35);
Integer age = Util.getValue(pair, "홍길동");
System.out.println(age);
ChildPair<String, Integer> childPair = new ChildPair<>("삼길동", 20);
Integer childAge = Util.getValue(pair, "이길동");
System.out.println(childAge);
// OtherPair는 Pair를 상속하지 않으므로 예외가 발생해야합니다.
/* OtherPair<String, Integer> otherPair = new OtherPair<>("삼길동, 20");
int otherAge = Util.getValue(otherPair, "삼길동");
System.out.println(otherAge); */
}
}
public class Pair<K, V> {
private K key;
private V value;
public K getKey() {
return key;
}
public V getValue() {
return value;
}
public Pair(K key, V value) {
this.key = key;
this.value = value;
}
}
public class ChildPair<K, V> extends Pair<K, V> {
public ChildPair(K key, V value) {
super(key, value);
}
}
public class OtherPair<K, V> {
private K key;
private V value;
public OtherPair(K key, V value) {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
public V getValue() {
return value;
}
}
public class Util {
// (제네릭 메소드를 작성해주세요)
}
실행결과는 다음과 같습니다.
35
null
<정답>
public static <T extends Pair, M> Integer getValue(T pair, M key){
if(pair.getKey().equals(key)){
return (Integer) pair.getValue();
}
else{
return null;
}
}
}
'[4기] 백엔드 개발자 부트캠프 "오르미" ~ing > [4기] 백엔드 개발자 부트캠프 오르미 수업 복습' 카테고리의 다른 글
| [4기] 46일차 Java ( Collection - Map, 람다식 ) (4) | 2024.02.16 |
|---|---|
| [4기] 45일차 Java ( Collection - list,set ) (0) | 2024.02.15 |
| [4기] 44일차 Java ( 제네릭2 ) (2) | 2024.02.14 |
| [4기] 38일차 Java ( 예외처리3, 제네릭 ) (0) | 2024.02.08 |
| [4기] 37일차 Java ( 예외처리, 트랜잭션 ) (2) | 2024.02.07 |