package com.paypal.edm.dsl.udf.test;
import java.util.ArrayList;
import java.util.List;
* @author mzang
*/
public class Test {
* 这个方法要遍历List<Human>里的元素,要求每个元素都要求是Human,或者其子类
* @param humans
*/
public static void showNameAndAddMoreNotWildGeneric(List<Human> humans){
humans.forEach(Human::getName);
humans.add(new Human());
}
* 这个方法要遍历List<? extends Human>里的元素,也要求每个元素都要求是Human,或者其子类
* @param humans
*/
public static void showNameWildGeneric(List<? extends Human> humans){
humans.forEach(Human::getName);
}
public static void testConsume() {
List<Parent> parentList = new ArrayList<>();
showNameWildGeneric(parentList);
}
public static void addMoreHumanWildGeneric(List<? super Parent> humans){
humans.add(new Child());
humans.add(new Parent());
}
public static void addMoreHumanNotWildGeneric(List<Parent> humans){
humans.add(new Child());
humans.add(new Parent());
humans.forEach(Human::getName);
}
public static void testProduce() {
List<Parent> parentList = new ArrayList<>();
addMoreHumanWildGeneric(parentList);
addMoreHumanNotWildGeneric(parentList);
List<GrandParent> childList = new ArrayList<>();
addMoreHumanWildGeneric(childList);
}
}
评论 (2 条评论)