반응형
제네릭 메소드의 이레이저 Erasure of Generic Methods
자바 컴파일러는 또한 제네릭 메소드 아규먼트에 있는 타입 패러미터를 지웁니다.
다름 제네릭 메소드를 검토하십시오:
The Java compiler also erases type parameters in generic method arguments. Consider the following generic method:
// Counts the number of occurrences of elem in anArray.
//
public static <T> int count(T[] anArray, T elem) {
int cnt = 0;
for (T e : anArray)
if (e.equals(elem))
++cnt;
return cnt;
}
Because T is unbounded, the Java compiler replaces it with Object:
public static int count(Object[] anArray, Object elem) { int cnt = 0; for (Object e : anArray) if (e.equals(elem)) ++cnt; return cnt; }
Suppose the following classes are defined:
class Shape { /* ... */ } class Circle extends Shape { /* ... */ } class Rectangle extends Shape { /* ... */ }
You can write a generic method to draw different shapes:
public static <T extends Shape> void draw(T shape) { /* ... */ }
The Java compiler replaces T with Shape:
public static void draw(Shape shape) { /* ... */ }
반응형
'Java 배우기' 카테고리의 다른 글
Non-Reifiable Types (0) | 2016.01.24 |
---|---|
Effects of Type Erasure and Bridge Methods (0) | 2016.01.24 |
Erasure of Generic Types 제네릭 타입의 이레이저 (0) | 2016.01.24 |
Type Erasure 이레이저 타입 (0) | 2016.01.24 |
Guidelines for Wildcard Use 와일드카드 사용 지침 (0) | 2016.01.24 |
댓글