java泛型中 的区别

时间:2022-09-27 15:55:13

(1)<? super String> is any class which is a superclass of String (including String itself). (In this case, the only other suitable class is Object.)

即包括String的父类和它本身的类。

(2) <? extends String> (which in this specific case wouldn't be very useful as String is final, so it can have no subclasses).

即包括String的子类,而String没有子类,为null。

 

QUESTION 217 Given:

3. import java.util.*;

4. public class Hancock {

5. // insert code here

6. list.add("foo");

7. }

8. }

Which two code fragments, inserted independently at line 5, will compile without warnings? (Choose two.)

A. public void addStrings(List list) {

B. public void addStrings(List<String> list) {

C. public void addStrings(List<? super String> list) {

D. public void addStrings(List<? extends String> list) {

Answer: BC