19-2: JAVA generic

jk·2024년 1월 26일
0

kdt 풀스택

목록 보기
36/127



1. 지네릭(Generic)이란?

  • type parameter(parameterized types) in class head<T> {}
  • It sets type first then construct class instance.
  • It can prevent object oriented class errors.
  • The type parameter is not one of primitive types(wrapper class is ok).



2.Generic 이전의 코드의 문제 상황들을 예시로 설명하시오.

//
//code
//
class Box {
    private Object obj;
    Box(Object obj) {
        this.obj = obj;
    }
    Object get() {
        return this.obj;
    }
}
class A {
    A() {
    }
    @Override
    public String toString() {
        return "A";
    }
}
class PreGeneric {
    public static void main(String[] args) {
        Box boxForClassA = new Box(new A());
        System.out.println(boxForClassA.get());
        Box boxForStringA = new Box("A");
        System.out.println(boxForStringA.get());
    }
}
/*print
A
A
*/



3. 아래의 결과가 나오도록 프로그래밍을 완성 하시오.

class DDBoxDemo {
    public static void main(String[] args) {
        DBox<String, Integer> box1 = new DBox<>();
        box1.set("Apple", 25);
//
        DBox<String, Integer> box2 = new DBox<>();
        box2.set("Orange", 33);
//        
        DDBox<DBox<String, Integer>, DBox<String, Integer>> ddbox = new DDBox<>();
        ddbox.set(box1, box2);
//
        System.out.println(ddbox);
    }
}
/*==================
Apple & 25
Orange & 33
*/
//
//code
//
class Const {
    static final String AND = " & ";
    static final String NEW_LINE = "\n";
}
class Functions {
    static String concatTwoWith(Object obj1, Object obj2, String bond) {
        String str1 = String.valueOf(obj1);
        String str2 = String.valueOf(obj2);
        return (str1.concat(bond).concat(str2));
    }
}
interface Box<T1, T2> {
    public abstract void set(T1 t1, T2 t2);
}
class DBox<T1 extends String, T2 extends Integer> implements Box<T1, T2> {
    private T1 t1;
    private T2 t2;
    DBox() {
    }
    @Override
    public void set(T1 t1, T2 t2) {
        this.t1 = t1;
        this.t2 = t2;
    }
    @Override
    public String toString() {
        return (Functions.concatTwoWith(this.t1, this.t2, Const.AND));
    }
}
class DDBox<T1 extends DBox, T2 extends DBox> implements Box<T1, T2> {
    private T1 t1;
    private T2 t2;
    DDBox() {
    }
    @Override
    public void set(T1 t1, T2 t2) {
        this.t1 = t1;
        this.t2 = t2;
    }
    @Override
    public String toString() {
        return (Functions.concatTwoWith(this.t1, this.t2, Const.NEW_LINE));
    }
}
/*print
Apple & 25
Orange & 33
*/



4. 아래와 같이 출력값이 나오도록 프로그래밍 하시오.

public static void main(String[] args) {
    Box<Integer> box1 = new Box<>();
    box1.set(99);
//
    Box<Integer> box2 = new Box<>();
    box2.set(55);
//
    System.out.println(box1.get() + " & " + box2.get());
    swapBox(box1, box2);
    System.out.println(box1.get() + " & " + box2.get());
}
/*==========
99 & 55
55 & 99
*/
//
//code
//
class SwapBox {
    public static void main(String[] args) {
        Box<Integer> box1 = new Box<>();
        box1.set(99);
    //
        Box<Integer> box2 = new Box<>();
        box2.set(55);
    //
        System.out.println(box1.get() + " & " + box2.get());
        swapBox(box1, box2);
        System.out.println(box1.get() + " & " + box2.get());
    }
    private static <T extends Integer> void swapBox(Box<T> box1, Box<T> box2) {
        T tBox1 = box1.get();
        T tBox2 = box2.get();
        box1.set(tBox2);
        box2.set(tBox1);
    }
}
class Box<T> {
    private T obj;
    Box() {
    }
    void set(T obj) {
        this.obj = obj;
    }
    T get() {
        return obj;
    }
}
/*print
99 & 55
55 & 99
*/
profile
Brave but clumsy

0개의 댓글