【发布时间】:2021-07-06 12:11:48
【问题描述】:
我有三个类AdminAccount、CustomerAccount、ConfectionerAccount,它们是从Account 扩展而来的。在 findByUsername(String username) 方法中,我想检查这三个类并返回不为 NULL 的那个。
public <R extends Account> R findByUsername(String username) {
AdminAccount adminAccount = adminAccountDao.findByUsername(username).orElse(null);
CustomerAccount customerAccount = customerAccountDao.findByUsername(username).orElse(null);
ConfectionerAccount confectionerAccount = confectionerAccountDao.findByUsername(username).orElse(null);
return Stream.of(adminAccount, customerAccount, confectionerAccount).filter(Objects::nonNull).findFirst().get();
}
但是当我尝试返回时,我得到了
Required type: R
Provided: Account
请您解释一下,我的错误在哪里?如何返回具体类,如AdminAccount、CustomerAccount 或ConfectionerAccount?
【问题讨论】:
标签: java generics inheritance