로우여 바운디드 와일드카드 Lower Bounded Wildcards
The Upper Bounded Wildcards section shows that an upper bounded wildcard restricts the unknown type to be a specific type or a subtype of that type and is represented using the extends keyword. In a similar way, a lower bounded wildcard restricts the unknown type to be a specific type or a super type of that type.
A lower bounded wildcard is expressed using the wildcard character ('?'), following by the super keyword, followed by its lower bound: <? super A>.
Note: You can specify an upper bound for a wildcard, or you can specify a lower bound, but you cannot specify both.
Say you want to write a method that puts Integer objects into a list. To maximize flexibility, you would like the method to work on List<Integer>, List<Number>, and List<Object> — anything that can hold Integer values.
To write the method that works on lists of Integer and the supertypes of Integer, such as Integer, Number, and Object, you would specify List<? super Integer>. The term List<Integer> is more restrictive than List<? super Integer> because the former matches a list of type Integer only, whereas the latter matches a list of any type that is a supertype of Integer.
The following code adds the numbers 1 through 10 to the end of a list:
public static void addNumbers(List<? super Integer> list) { for (int i = 1; i <= 10; i++) { list.add(i); } }
The Guidelines for Wildcard Use section provides guidance on when to use upper bounded wildcards and when to use lower bounded wildcards.
« DopZaClub »
'Java 배우기' 카테고리의 다른 글
Wildcard Capture and Helper Methods 와일드카드 캡처 및 헬퍼 메소스 (0) | 2016.01.24 |
---|---|
Wildcards and Subtyping 와일드카드 및 서브타이핑 (0) | 2016.01.24 |
Unbounded Wildcards 언바운디드 와일드카드 (0) | 2016.01.24 |
Upper Bounded Wildcards 어퍼 바운디드 와일드카드 (0) | 2016.01.24 |
Wildcards 와일드카드 (0) | 2016.01.24 |
댓글