checkout.enterItem(new Rice("Basmati Rice", 2.25, 399));
That code is invoking the enterItem
method and passing a Rice
as an argument.
public void enterItem(){
items.add()
}
The enterItem
method does not accept any arguments, so that won't work. You could do this...
public void enterItem(Rice rice){
items.add(rice);
}
That will work for Rice
, but not for Baguette
(unless Baguette
extends Rice
). You could add 2 versions of enterItem
:
public void enterItem(Rice rice){
items.add(rice);
}
public void enterItem(Baguette bag){
items.add(bag);
}
If Rice
and Bagquette
share something above them in the inheritance/implements hierarchy, something like this could work:
public class Baguette implements Edible {}
public class Rice implements Edible {}
public void enterItem(Edible edible){
items.add(edible);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…