본문 바로가기
개념

[이것이 자바다] chapter.13 확인 문제

by cook_code 2023. 4. 3.
반응형

1.  제네릭에 대한 설명으로 틀린 것은 무엇입니까?

  1. 컴파일 시 강한 타입 체크를 할 수 있다. 
  2. 타입 변환(casting)을 제거한다. 
  3. 제네릭 타입은 타입 파라미터를 가지는 제네릭 클래스와 인터페이스를 말한다. 
  4. 제네릭 메소드는 리턴 타입으로 타입 파라미터를 가질 수 없다. 

2. ContainerExample 클래스의 main() 메소드는 Container 제네릭 타입을 사용하고 있습니다. main() 메소드에서 사용하는 방법을 참고해서 Container 제네릭 타입을 선언해보세요.

package org.chapter13;


import java.io.PrintWriter;

public class ContainerExample {
    public static void main(String[] args) {
        Container<String> container1 = new Container<String>();
        container1.set("홍길동");
        String str = container1.get();

        Container<Integer> container2 = new Container<Integer>();
        container2.set(6);
        int value = container2.get();
    }

    private static class Container<T> {
        private T t;

        public void set(T t) {
            this.t = t;
        }

        public T get() {
           return t;
        }
    }
}

3. ContainerExample 클래스의 main() 메소드는 Container 제네릭 타입을 사용하고 있습니다. main() 메소드에서 사용하는 방법을 참고해서 Container 제네릭 타입을 선언해보세요.

package org.chapter13;

public class Container<K, V> {
    private K key;
    private V value;

    public Container(K key, V value) {
        this.key = key;
        this.value = value;
    }

    public Container() {
    }

    public K getKey() {
        return key;
    }

    public V getValue() {
        return value;
    }

    public void set(K key, V value) {
        this.key = key;
        this.value = value;
    }
}

4. Util.getValue() 메소드는 첫 번째 매개값으로 Pair 타입과 하위 타입만 받고, 두 번째 매개값으로 키 값을 받습니다. 리턴 값은 키 값이 일치할 경우 Pair에 저장된 값을 리턴하고, 일치하지 않으면 null을 리턴하도록 getValue() 제네릭 메소드를 정의해보세요.

package org.chapter13;

import jdk.jshell.execution.Util;

public class UtilExample {
    public static void main(String[] args) {
        Pair<String, Integer> pair = new Pair<>("홍길동", 35);
        //문제랑 다른 점 양해바랍니다; 패키지명을 넣어야 오류가 안나서요:(
        Integer age = org.chapter13.Util.getValue(pair,"홍길동");
        System.out.println(age);

        ChildPair<String, Integer> childPair = new ChildPair<>("홍삼원", 20);
        Integer childAge = org.chapter13.Util.getValue(childPair,"홍삼순");
        System.out.println(childAge);

        /*OtherPair<String, Integer> otherPair = new OtherPair<String, Integer>("홍삼원", 20);
        //otherPair는 Pair을 상속하지 않으므로 예외가 발생해야 합니다.
        int otherPair = org.chapter13.Util.getValue(otherPair,"홍삼원");
        System.out.println(otherPair);*/
    }
}
package org.chapter13;

public class Pair<K, V> {
    private K key;
    private V value;

    public Pair(K key, V value) {
        this.key = key;
        this.value = value;
    }

    public K getKey() {
        return key;
    }

    public V getValue() {
        return value;
    }
}
package org.chapter13;

public class ChildPair<K, V> extends Pair<K,V>{
    public ChildPair(K k, V v){
        super(k,v);
    }
}
package org.chapter13;

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;
    }
}
반응형