관리 메뉴

Partially Committed

[CH12] Generics, Enumeration, Annotation λ³Έλ¬Έ

πŸ’» Study !/JAVA

[CH12] Generics, Enumeration, Annotation

WonderJay 2022. 8. 30. 11:06
728x90
λ°˜μ‘ν˜•
SMALL

λ³Έ ν¬μŠ€νŒ…μ€ μžλ°”μ˜ 정석 ꡐ재λ₯Ό κ³΅λΆ€ν•˜λ©°, κ°„λ‹¨νžˆ 정리/기둝 μš©λ„λ‘œ μž‘μ„±ν•˜μ˜€μŠ΅λ‹ˆλ‹€. ν˜Ήμ—¬, 잘λͺ»λœ λ‚΄μš©μ΄ μžˆλ‹€λ©΄ μ§€μ ν•΄μ£Όμ‹œλ©΄ κ°μ‚¬ν•˜κ² μŠ΅λ‹ˆλ‹€.

 

 


1. Generics

  Generics λŠ” λ‹€μ–‘ν•œ νƒ€μž…μ˜ 객체듀을 λ‹€λ£¨λŠ” λ©”μ„œλ“œλ‚˜ Collection class 에 compile μ‹œ type checking 을 ν•΄μ£ΌλŠ” κΈ°λŠ₯이닀. 객체 νƒ€μž…μ„ compile - time 에 check ν•˜λ―€λ‘œ ν”„λ‘œκ·Έλž¨ μ•ˆμ •μ„±(μ˜λ„μΉ˜ μ•Šμ€ νƒ€μž…μ˜ 객체가 μ €μž₯되고, 뢈러올 λ•Œ 잘λͺ»λœ μΊμŠ€νŒ…μœΌλ‘œ 인해 λ°œμƒν•˜λŠ” 였λ₯˜κ°€ 쀄어듦)이 높아지고 ν˜•λ³€ν™˜ λ“±μ˜ μž‘μ—…μ„ μ—†μ• μ€€λ‹€.  λ‹€λ£¨κ³ μž ν•˜λŠ” 객체의 νƒ€μž…μ„ 미리 λͺ…μ‹œν•˜μ—¬ νƒ€μž… μ•ˆμ •μ„±μ„ 높이고 μ½”λ“œκ°€ 간결해진닀.

 

  class Box<T> {

T item;

 

void setItem (T item) { this.item = item; }

T getItem() { return item; }

}

 

  μœ„μ™€ 같은 Generic class μ—μ„œ T λŠ” type variable 이라고 ν•œλ‹€. μœ„μ™€ 같이 μ„ μ–Έν•œ Box Class 의 Object λŠ” μ•„λž˜μ™€ 같이 생성할 수 μžˆλ‹€.

 

Box<String> b = new Box<String>();

b.setItem(new Object());

b.setItem("ABC");

string item = b.getItem();

 

  Box ν΄λž˜μŠ€λŠ” class Box<T> {  } μ™€ 같이 μ„ μ–Έλ˜μ–΄ μžˆλ‹€.  Box<T> λŠ” Generic class 이며 T의 Box ν˜Ήμ€ T Box 라고 μ½λŠ”λ‹€. T λŠ” type variable ν˜Ήμ€ type parameter 이며 Box λŠ” raw type(μ›μ‹œ νƒ€μž…) 이라고 λΆ€λ₯Έλ‹€. Box 객체 생성 μ‹œ, <T> μžλ¦¬μ— <String> 을 μ˜ˆμ‹œλ‘œ μ„ μ–Έν•˜μ˜€λŠ”λ°, μ΄λ•Œ String 은 parameterized type 이라고 ν•œλ‹€. 

 

  Limitiation of Generics 

T[] itemArr; 와 같이 λ°°μ—΄ νƒ€μž…μ˜ μ°Έμ‘° λ³€μˆ˜λ₯Ό μ„ μ–Έν•˜λŠ” 것은 κ°€λŠ₯ν•˜μ§€λ§Œ new T[10] κ³Ό 같이 배열을 μƒμ„±ν•˜λŠ” 것은 ν—ˆμš©λ˜μ§€ μ•ŠλŠ”λ‹€. Generic 은 run - time 쀑에 κ²°μ •λ˜λŠ”λ°, new operator 은 compile λ‹¨κ³„μ—μ„œ type κ°€ 무엇인지 μ •ν™•νžˆ μ•Œμ•„μ•Όλ§Œ ν•˜λ―€λ‘œ ν—ˆμš©ν•˜μ§€ μ•ŠλŠ” 것이며, instanceof operator 도 λ™μΌν•œ μ›λ¦¬λ‘œ μ•ˆλœλ‹€. generic array λ₯Ό 생성해야 ν•˜λŠ” ν•„μš”κ°€ μžˆλ‹€λ©΄, new operator λŒ€μ‹ μ— Reflection API 의 newInstance() 처럼 동적 객체 생성 λ©”μ„œλ“œλ‘œ 배열을 λ§Œλ“€κ±°λ‚˜, Object 배열을 μƒμ„±ν•œ λ‹€μŒμ— T[] 둜 ν˜•λ³€ν™˜ν•˜λŠ” 것이 λŒ€μ•ˆμ΄λ‹€.

 

  Generic class 의 객체 생성과 μ‚¬μš©

import java.util.ArrayList;

class source{
    public static void main(String[] args) {
        Box<Fruit> fruitBox = new Box<Fruit>();
        Box<Apple> appleBox = new Box<Apple>();
        Box<Toy> toyBox = new Box<Toy>();

        fruitBox.add(new Fruit());
        fruitBox.add(new Apple());

        appleBox.add(new Apple());
        appleBox.add(new Apple());

        toyBox.add(new Toy());

        System.out.println(fruitBox);
        System.out.println(appleBox);
        System.out.println(toyBox);

    }
}

class Fruit { public  String toString() { return "Fruit"; }}
class Apple extends Fruit { public  String toString() { return "Apple"; }}
class Grape extends Fruit { public  String toString() { return "Grape"; }}
class Toy { public  String toString() { return "Toy"; }}

class Box<T>{
    ArrayList<T> list = new ArrayList<T>();
    void add(T item) { list.add(item);}
    T get(int i) { return list.get(i);}
    int size() { return list.size();}
    public String toString(){ return list.toString();}
}

 

  μ œν•œλœ Generic class

type 문자둜 μ‚¬μš©ν•  type λ₯Ό λͺ…μ‹œν•˜λ©΄ ν•œ μ’…λ₯˜μ˜ type 만 μ €μž₯ν•  수 μžˆλ„λ‘ μ œν•œν•  수 μžˆλ‹€. ν•˜μ§€λ§Œ type μ—λŠ” μ œν•œμ΄ μ—†λŠ”λ°, νƒ€μž… λ§€κ°œλ³€μˆ˜ T 에 지정할 수 μžˆλŠ” νƒ€μž…μ˜ μ’…λ₯˜λ₯Ό μ œν•œν•  수 μ—†μ„κΉŒ? μ΄λŠ” generic νƒ€μž…μ— extends ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•˜λ©΄ νŠΉμ • νƒ€μž…μ˜ μžμ†λ“€λ§Œ λŒ€μž…ν•  수 μžˆλ„λ‘ μ œν•œν•  수 μžˆλ‹€. ν΄λž˜μŠ€κ°€ μ•„λ‹Œ μΈν„°νŽ˜μ΄μŠ€λ₯Ό κ΅¬ν˜„ν•΄μ•Ό ν•œλ‹€λŠ” μ œμ•½μ΄ ν•„μš”ν•˜λ”λΌλ„ implements ν‚€μ›Œλ“œκ°€ μ•„λ‹Œ extends ν‚€μ›Œλ“œλ₯Ό μ‚¬μš©ν•¨μ— μ£Όμ˜ν•˜μž.

import java.util.ArrayList;

class Fruit implements Eatable{
    public String toString() { return "Fruit"; }
}

interface Eatable{ }

class FruitBox<T extends Fruit & Eatable> extends Box<T> {}

class source{
    public static void main(String[] args) {
        FruitBox<Fruit> fruitBox = new FruitBox<Fruit>();
        FruitBox<Apple> appleBox = new FruitBox<>();
        FruitBox<Grape> grapeBox = new FruitBox<>();

        fruitBox.add(new Fruit());
        fruitBox.add(new Apple());
        fruitBox.add(new Grape());

        appleBox.add(new Apple());
        grapeBox.add(new Grape());

        System.out.println("fruitBox-" + fruitBox);
        System.out.println("appleBox-" + appleBox);
        System.out.println("grapeBox-" + grapeBox);
    }
}

 

  μ™€μΌλ“œ μΉ΄λ“œ

Generic νƒ€μž…μ΄ λ‹€λ₯Έ 것 λ§ŒμœΌλ‘œλŠ” μ˜€λ²„λ‘œλ”©μ΄ μ„±λ¦½ν•˜μ§€ μ•ŠλŠ”λ‹€.Generic νƒ€μž…μ„ λ‹€λ₯΄κ²Œ λ©”μ†Œλ“œλ₯Ό κ΅¬ν˜„ν•˜λ €λ©΄ μ™€μΌλ“œ μΉ΄λ“œλ₯Ό μ‚¬μš©ν•œλ‹€. 

 

<? extends T> μ™€μΌλ“œ μΉ΄λ“œμ˜ μƒν•œ μ œν•œ. T와 κ·Έ μžμ†λ“€λ§Œ κ°€λŠ₯

<? super T> μ™€μΌλ“œ μΉ΄λ“œμ˜ ν•˜ν•œ μ œν•œ. T와 κ·Έ μ‘°μƒλ“€λ§Œ κ°€λŠ₯

<?> μ œν•œ μ—†μŒ. λͺ¨λ“  νƒ€μž…μ΄ κ°€λŠ₯ν•˜λ©° <? extends Object> 와 동일함

 

import java.util.ArrayList;

class Fruit implements Eatable{
    public String toString() { return "Fruit"; }
}
interface Eatable{ }
class FruitBox<T extends Fruit & Eatable> extends Box<T> {}

class Juicer {
    static Juice makeJuice(FruitBox<? extends Fruit> box){
        String tmp = "";
        for(Fruit f : box.getList())
            tmp += f + " ";
        return new Juice(tmp);
    }
}
class Juice{
    String name;
    Juice(String name){this.name = name + "Juice";}
    public String toString() {return name;}
}

class source{
    public static void main(String[] args) {
        FruitBox<Fruit> fruitBox = new FruitBox<Fruit>();
        FruitBox<Apple> appleBox = new FruitBox<>();

        fruitBox.add(new Apple());
        fruitBox.add(new Grape());
        appleBox.add(new Apple());
        appleBox.add(new Apple());

        System.out.println(Juicer.makeJuice(fruitBox));
        System.out.println(Juicer.makeJuice(appleBox));
    }
}

class Apple extends Fruit { public  String toString() { return "Apple"; }}
class Grape extends Fruit { public  String toString() { return "Grape"; }}
class Toy { public  String toString() { return "Toy"; }}

class Box<T>{
    ArrayList<T> list = new ArrayList<T>();
    void add(T item) { list.add(item);}
    T get(int i) { return list.get(i);}
    int size() { return list.size();}
    ArrayList<T> getList() { return list; }
    public String toString(){ return list.toString();}
}

 

  Generic method

λ©”μ„œλ“œ 선언뢀에 generic type 이 μ„ μ–Έλœ method λ₯Ό λ§ν•œλ‹€. ν•˜λ‚˜ μœ μ˜ν•  점은 Generic method λ₯Ό ν˜ΈμΆœν•  λ•Œ, λŒ€μž…λœ type λ₯Ό μƒλž΅ν•  수 μ—†λŠ” κ²½μš°μ—λŠ” μ°Έμ‘°λ³€μˆ˜λ‚˜ 클래슀 이름도 μƒλž΅ν•  수 μ—†λ‹€λŠ” 것이닀. Generic method λ₯Ό μ΄μš©ν•˜λ©΄ λ§€κ°œλ³€μˆ˜μ˜ type 이 λ³΅μž‘ν•œ 경우 μœ μš©ν•˜λ‹€. 

 

2. Enumeration

   μ—΄κ±°ν˜• Direction μ΄ μ•„λž˜μ²˜λŸΌ μ •μ˜λ˜μ–΄μžˆλ‹€κ³  κ°€μ •ν•˜μž.

 

enum Direction { EAST, SOUTH, WEST, NORTH }

 

μœ„μ˜ μ—΄κ±°ν˜• μƒμˆ˜ 각각이 Direction 객체이며, μœ„λ₯Ό class 둜 μ •μ˜ν•˜κ³ μž ν•œλ‹€λ©΄ μ•„λž˜μ™€ 같을 것이닀.

 

class Direction {

static final Direction EAST = new Direction("EAST");

static final Direction SOUTH = new Direction("SOUTH");

static final Direction WEST = new Direction("WEST ");

static final Direction NORTH = new Direction("NORTH ");

 

private String name;

private Direction(String name) { this.name = name; }

}

 

  Direction Class 의 static μƒμˆ˜ EAST, SOUTH, WEST, NORTH 의 값은 객체의 μ£Όμ†Œμ΄κ³  λ°”λ€Œμ§€ μ•ŠμœΌλ―€λ‘œ == 둜 비ꡐ가 κ°€λŠ₯ν•˜λ‹€. λ˜ν•œ λͺ¨λ“  μ—΄κ±°ν˜•μ€ abstract class Enum μ˜ μžμ†μ΄λ―€λ‘œ, λŒ€λž΅ μ•„λž˜μ™€ 같이 κ΅¬ν˜„λ˜μ–΄μžˆλ‹€.

 

abstract class MyEnum <T extends MyEnum<T>> implements Comparable<T> {

static int id = 0;

int ordinal;

String name = "";

public int ordinal() { return ordinal; }

MyEnum(String name){

this.name = name;

ordinal = id++;

}

public int compareTo(T t){

return ordinal - t.ordinal();

     }

}

 

  MyEnum 클래슀λ₯Ό <T extends MyEnum<T>> 와 같이 ν•΄μ£Όμ—ˆκΈ° λ•Œλ¬Έμ—, νƒ€μž… T λŠ” 무쑰건 MyEnum<T> 의 μžμ†μ΄μ–΄μ•Ό ν•˜λŠ” μ œμ•½μ΄ 걸리게 되고, μ΄λŸ¬ν•œ 이유둜 compareTo λ₯Ό κ°„λ‹¨ν•˜κ²Œ μž‘μ„±ν•  수 μžˆμ—ˆλ˜ 것이닀. 

 

 

3. Annotation

  μ†ŒμŠ€μ½”λ“œ μ•ˆμ— μ£Όμ„μ²˜λŸΌ ν”„λ‘œκ·Έλž¨μ— 영ν–₯을 주지 μ•ŠμœΌλ©΄μ„œλ„ λ‹€λ₯Έ ν”„λ‘œκ·Έλž¨μ—κ²Œ μœ μš©ν•œ 정보λ₯Ό μ œκ³΅ν•˜λŠ” λ©”λͺ¨λ₯Ό λ§ν•œλ‹€.

 

ν‘œμ€€ μ• λ„ˆν…Œμ΄μ…˜

1. @Override

2. @Deprecated

3. @SuppressWarnings

4. @SafeVarargs

5. @Functionalinterface

6. @Native

 

메타 μ• λ„ˆν…Œμ΄μ…˜

7. @Taget

8. @Documented

9. @Inherited

10. @Retention

11. @Repeatable

 


References

http://www.yes24.com/Product/Goods/24259565

 

Java의 정석 - YES24

졜근 7λ…„λ™μ•ˆ μžλ°” λΆ„μ•Όμ˜ 베슀트 μ…€λŸ¬ 1μœ„λ₯Ό μ§€μΌœμ˜¨ `μžλ°”μ˜ 정석`의 μ΅œμ‹ νŒ. μ €μžκ°€ μΉ΄νŽ˜μ—μ„œ 12λ…„κ°„ 직접 λ…μžλ“€μ—κ²Œ 닡변을 ν•΄μ˜€λ©΄μ„œ μ΄ˆλ³΄μžκ°€ μ–΄λ €μ›Œν•˜λŠ” 뢀뢄을 잘 νŒŒμ•…ν•˜κ³  μ“΄ μ±…. 뿐만 μ•„

www.yes24.com

 

 

728x90
λ°˜μ‘ν˜•
LIST

'πŸ’» Study ! > JAVA' μΉ΄ν…Œκ³ λ¦¬μ˜ λ‹€λ₯Έ κΈ€

[CH14] Lambda & Stream  (0) 2022.09.03
[CH11] Collections framework  (2) 2022.08.27
[CH09] java.lang νŒ¨ν‚€μ§€  (0) 2022.08.21
[CH08] μ˜ˆμ™Έμ²˜λ¦¬(Exception handling)  (0) 2022.08.02
[CH07] 객체지ν–₯ν”„λ‘œκ·Έλž˜λ°4 (OOP)  (0) 2022.08.02
Comments