首页 > 解决方案 > Java - 将子类赋予以父类为参数的方法

问题描述

ABTeacher并且ABStudent是子类ABUser

applyStudentChange我有Collection<ABStudent> ABStudents

我想打电话 changeABUser(Collection<ABUser> ABUsers)applyStudentChange 但我得到的集合是子类的集合,我应该怎么打电话changeABUser(Collection<ABUser> ABUsers)

我试过这样打电话changeABUser((Collection<ABUser>) ABStudents),这个演员表似乎不起作用......

标签: javainheritance

解决方案


您需要为您的集合提供以下成员类型:

.... changeABUser(Collection<? extends ABUser> users) ....

推荐阅读