ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 15. demo2 - (4) FruitDemo ( ☆ 제네릭 메서드)
    java/코드 리뷰 2020. 4. 24. 15:59
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    package demo2;
     
    public class Fruit {
        
        @Override
        public String toString() {
            return "과일";
        }
        
    }
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    package demo2;
     
    public class Apple extends Fruit {
        
        public String toString() {
            return "사과";
        }
        
    }
     
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    package demo2;
     
    public class Grape extends Fruit {
        
        public String toString() {
            return "포도";
        }
        
    }
     
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    package demo2;
     
    import java.util.ArrayList;
     
    public class FruitBox<T> {
        
        private ArrayList<T> list = new ArrayList<T>();
        
        public void add(T t) {
            list.add(t);
        }
        
        public T get(int index) {
            return list.get(index);
        }
        
        public int size() {
            return list.size();
        }
        
    }
     
     
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    package demo2;
     
    public class Mixer {
        
        // 제네릭 메서드
        // 메서드가 여러 종류의 객체를 다루는 경우 (여러 객체를 매개변수로 다루거나,
        // (매개변수로 전달받아야 하는 경우) 제네릭 메서드로 만듬
        
        // 메서드의 리턴타입 앞체 제네릭 선언
        // 메서드 호출 전달 받은 객체의 타입 지점에서 T를 선언
        
        public static <T> void mix(FruitBox<T> box) {
            int size = box.size();
            for (int i=0; i<size; i++) {
                T t = box.get(i);
                System.out.println(t.toString() + "를 믹싱합니다.");
                
            }
            System.out.println();
        }
        
    }
     
     
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    package demo2;
     
    public class FruitDemo {
        
        public static void main(String[] args) {
            
            // Fruit, Apple, Grape 객체를 담는 과일 상자
            FruitBox<Fruit> box1 = new FruitBox<Fruit>();
            box1.add(new Fruit());
            box1.add(new Apple());
            box1.add(new Grape());
     
            // Apple만 담는 과일 상자
            FruitBox<Apple> box2 = new FruitBox<Apple>();
    //        box2.add(new Fruit());
            box2.add(new Apple());
    //        box2.add(new Grape());
     
            // Grape만 담는 과일 상자
            FruitBox<Grape> box3 = new FruitBox<Grape>();
    //        box3.add(new Fruit());
    //        box#.add(new Apple());
            box3.add(new Grape());
            
            Mixer.mix(box1); // 과일박스<Fruit>
            Mixer.mix(box2); // 과일박스<Apple>
            Mixer.mix(box3); // 과일박스<Grape>
     
        }
    }
     
     
Designed by Tistory.