我对自己正在构建的类的意外行为(某种复合模式)感到非常惊讶。
这是重点和涉及的课程:
IGroupable
是具有2种方法的接口
公共接口IGroupable {
BigDecimal getTotal();
Boolean isManuallySettlable();
}
Event
类实现IGroupable
@实体 @Table(name =“ V_ALL_BUSINESS_EVENTS”) 公共类EventView实现了IGroupable,可序列化的{
@Id
@Column(name="UNIQUE_ID")
private long eventId;
...
@Override
public BigDecimal getTotal() {
return this.eventGrossAmount;
}
@Override
public Boolean isManuallySettlable() {
return (this.eventIsManuallySettlable == null || this.eventIsManuallySettlable == false) ? false : true;
}
AbstractGroup
抽象类实现IGroupable
:
公共抽象类AbstractGroup实现IGroupable,可序列化的{
private static final long serialVersionUID = 1L;
protected int nbRows;
protected transient List<IGroupable> groups;
...
CurrencyGroup
,BuyerPlatformGroup
和CounterpartExecDateGroup
都扩展了AbstractGroup
,所以每个都有一个List<IGroupable> groups
属性
公共类CurrencyGroup扩展了AbstractGroup {
protected String currency;
// --------------------------------------------------------
public CurrencyGroup(String currency) {
this.currency = currency;
super.groups = new ArrayList<>();
}
...
@Override
public BigDecimal getTotal() {
BigDecimal total = new BigDecimal(0);
total = this.groups.stream()
.map(groupable -> groupable.getTotal())
.reduce(total, BigDecimal::add);
return total;
}
@Override
public Boolean isManuallySettlable() {
boolean isSettlable = true;
for(IGroupable g : groups) {
if(!g.isManuallySettlable()) {
isSettlable = false;
}
}
return isSettlable;
}
公共类BuyerPlatformGroup扩展了AbstractGroup {
protected String buyerName;
protected String buyerRTS;
protected String platform;
// --------------------------------------------------------
public BuyerPlatformGroup(String buyerName, String buyerRTS, String platform) {
this.buyerName = buyerName;
this.buyerRTS = buyerRTS;
this.platform = platform;
super.groups = new ArrayList<>();
}
...
@Override
public BigDecimal getTotal() {
BigDecimal total = new BigDecimal(0);
total = this.groups.stream()
.map(groupable -> groupable.getTotal())
.reduce(total, BigDecimal::add);
return total;
}
@Override
public Boolean isManuallySettlable() {
boolean isSettlable = true;
for(IGroupable g : groups) {
if(!g.isManuallySettlable()) {
isSettlable = false;
}
}
return isSettlable;
}
}
公共类CounterpartExecDateGroup扩展了AbstractGroup {
protected String counterpart;
protected Date execDate;
protected BigDecimal amount;
protected String step;
protected String operationType;
// --------------------------------------------------------
public CounterpartExecDateGroup() {
super.groups = new ArrayList<>();
}
...
@Override
public BigDecimal getTotal() {
BigDecimal total = new BigDecimal(0);
total = groups.stream()
.map(groupable -> groupable.getTotal())
.reduce(total, BigDecimal::add);
return total;
}
@Override
public Boolean isManuallySettlable() {
boolean isSettlable = true;
for(IGroupable g : groups) {
if(!g.isManuallySettlable()) {
isSettlable = false;
}
}
return isSettlable;
}
}
->它们都是从'AbstractGroup'继承的,并且都是'IGroupable'对象
@Test
public void allIGroupableTest() {
List<EventView> events = eventDao.findAll();
EventView event = (EventView) events.get(0);
CurrencyGroup ccyGrp = new CurrencyGroup("EUR");
BuyerPlatformGroup buyerGrp = new BuyerPlatformGroup(event.getEventBuyerName(), event.getEventRTSCode(), event.getEventPlatform());
CounterpartExecDateGroup counterpartGrp = new CounterpartExecDateGroup();
ccyGrp.addGroup(buyerGrp); // OK
buyerGrp.addGroup(counterpartGrp); // OK
counterpartGrp.setGroups(events); **// ERROR**
但是我遇到一个错误,告诉我:类型setGroups(List<IGroupable>)
的方法AbstractGroup
不适用于参数(List<EventView>)
。
为什么会这样表现?
由于Event
类实现了IGroupable
接口,因此IGroupable
对象列表应该能够接受Events
列表,不是吗?