Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
多态性java循环错误_Java_Loops_Polymorphism - Fatal编程技术网

多态性java循环错误

多态性java循环错误,java,loops,polymorphism,Java,Loops,Polymorphism,我遇到了一个关于循环内多态调用的问题 我有一个名为Item的抽象类,它有两个子类ClothingItem和SportItem,还有一个名为printBudgetGST(Items[/Item)的抽象方法,用于返回包含税在内的更新定价的项目字符串 项目类别: public abstract class Item { private int code; private double price; private boolean isOnGST; public Item() { } public

我遇到了一个关于循环内多态调用的问题

我有一个名为
Item
的抽象类,它有两个子类
ClothingItem
SportItem
,还有一个名为
printBudgetGST(Items[/Item)
的抽象方法,用于返回包含税在内的更新定价的项目字符串

项目类别:

public abstract class Item 
{
private int code;
private double price;
private boolean isOnGST;

public Item()
{

}
public Item(int code,double price,boolean isOnGST)
{
    this.code = code;
    this.price = price;
    this.isOnGST = isOnGST;
}
public void setGST(boolean isgst)
{
    this.isOnGST = isgst;
}
public int getCode()
{
    return code;
}
public boolean getIsOnGST()
{
    return isOnGST;
}
public double getCurrentPrice()
{
    return price;
}
public String toString() {
    return "Item [code=" + code + ", price=" + price + ", isOnGST=" + isOnGST + "]";
}
public abstract String printBudgetGST(Item[] items);
}
服装类

public class ClothingItem extends Item 
{
public ClothingItem(){

}
public ClothingItem(int code,double price,boolean isOnGST)
{
    super(code,price,isOnGST);
}
@Override
public String printBudgetGST(Item[] item) 
{
    String stringitem ="";
    for(int i=0;i<item.length;i++)
    {
        if(item[i].getIsOnGST()==true&&item[i].getCurrentPrice()<100.00)
            {
                 double finalprice =(0.06*item[i].getCurrentPrice())+item[i].getCurrentPrice();
                 stringitem = stringitem + " " + "ClothingItem : " + item[i].getCode()+":"+"RM"+finalprice;
            }   
    }

    return stringitem;
}
}
公共类ClothingItem扩展项目
{
公共服装项目(){
}
公共服装项目(整数代码,双倍价格,布尔isOnGST)
{
超级(代码、价格、isOnGST);
}
@凌驾
公共字符串printBudgetGST(项[]项)
{
字符串stringitem=“”;

对于(int i=0;i问题在于,您正在传递给
PrintBudgetSt
整个项目数组,并在
PrintBudgetSt
的实现中迭代该数组。相反,您应该删除该参数,在
PrintBudgetSt
中,您只需调用
getCurrentPrice()
getCode()
这个
上,而不是在每个
项[i]

此外,您正在检查item子类中的最高价格(<100或<150),但最好与
printItem
中的其他检查一起进行,因为最高价格取决于子类(
SportsItem
vs
ClothinItem
)我建议您在
Item
中创建一个抽象方法
boolean isOnBudget()
,并在这两个子类中相应地实现


您的代码的完全固定版本是

public abstract class Item {
    private int code;
    private double price;
    private boolean isOnGST;

    public Item()
    {

    }
    public Item(int code,double price,boolean isOnGST)
    {
        this.code = code;
        this.price = price;
        this.isOnGST = isOnGST;
    }
    public void setGST(boolean isgst)
    {
        this.isOnGST = isgst;
    }
    public int getCode()
    {
        return code;
    }
    public boolean getIsOnGST()
    {
        return isOnGST;
    }
    public double getCurrentPrice()
    {
        return price;
    }
    public String toString() {
        return "Item [code=" + code + ", price=" + price + ", isOnGST=" + isOnGST + "]";
    }
    public abstract String printBudgetGST();
    public abstract boolean isOnBudget();
}

class ClothingItem extends Item {
    public ClothingItem() {

    }

    public ClothingItem(int code, double price, boolean isOnGST) {
    super(code, price, isOnGST);
    }

    @Override
    public String printBudgetGST() {
    String stringitem = "";
    double finalprice = (0.06 * getCurrentPrice()) + getCurrentPrice();
    stringitem = stringitem + " " + "ClothingItem : " + getCode() + ":" + "RM" + finalprice;

    return stringitem;
    }

    @Override
    public boolean isOnBudget() {
    return getCurrentPrice() < 100.00;
    }
}

class SportsItem extends Item {
    public SportsItem() {
    }

    public SportsItem(int code, double price, boolean isOnGST) {
    super(code, price, isOnGST);
    }

    public String printBudgetGST() {
    String stringitem = "";
    double finalprice = (0.06 * getCurrentPrice()) + getCurrentPrice();
    stringitem = stringitem + "SportsItem : " + getCode() + ":" + "RM" + finalprice;
    return stringitem;
    }

    @Override
    public boolean isOnBudget() {
    return getCurrentPrice() < 150.00;
    }
}

class Retail_Item
{
    private Item[] itemList;

    public Retail_Item()
    {
    itemList = new Item[10];
    itemList[0] = new ClothingItem(10001,85,true);
    itemList[1] = new ClothingItem(10002,150,false);
    itemList[2] = new ClothingItem(10003,168,true);
    itemList[3] = new ClothingItem(10004,43,true);
    itemList[4] = new ClothingItem(10005,162,false);
    itemList[5] = new SportsItem(10006,178,false);
    itemList[6] = new SportsItem(10007,80,true);
    itemList[7] = new SportsItem(10008,191,false);
    itemList[8] = new SportsItem(10009,45,true);
    itemList[9] = new SportsItem(10010,121,true);
    }

    public  void printItem() {
    for(int i =0 ;i<itemList.length;i++) {
        if(itemList[i].getIsOnGST()==true && itemList[i].printBudgetGST().length()>0 && itemList[i].isOnBudget())
        {

        System.out.println(itemList[i].printBudgetGST());
        }
    }
    }
}

class TestRetailItem {
    public static void main(String[] args) {
    Retail_Item ret = new Retail_Item();
    ret.printItem();
    }
}
公共抽象类项{
私有整数码;
私人双价;
私有布尔isOnGST;
公共项目()
{
}
公共项目(整数代码、双倍价格、布尔isOnGST)
{
this.code=代码;
这个价格=价格;
this.isOnGST=isOnGST;
}
公共void setGST(布尔值isgst)
{
this.isOnGST=isgst;
}
公共int getCode()
{
返回码;
}
公共布尔getIsOnGST()
{
回归均衡;
}
公共双getCurrentPrice()
{
退货价格;
}
公共字符串toString(){
返回“项目[code=“+code+”,price=“+price+”,isOnGST=“+isOnGST+”]”;
}
公共抽象字符串printbughtgst();
公共抽象布尔值isOnBudget();
}
类ClothingItem扩展项目{
公共服装项目(){
}
公共服装项目(整数代码,双倍价格,布尔isOnGST){
超级(代码、价格、isOnGST);
}
@凌驾
公共字符串printbughtgst(){
字符串stringitem=“”;
双最终价格=(0.06*getCurrentPrice())+getCurrentPrice();
stringitem=stringitem++“ClothingItem:“+getCode()+”:“+”RM“+最终价格;
退货项目;
}
@凌驾
公共布尔值isOnBudget(){
返回getCurrentPrice()<100.00;
}
}
类SportsItem扩展项目{
公共体育项目({
}
公共体育项目(整数代码,双倍价格,布尔isOnGST){
超级(代码、价格、isOnGST);
}
公共字符串printbughtgst(){
字符串stringitem=“”;
双最终价格=(0.06*getCurrentPrice())+getCurrentPrice();
stringitem=stringitem+“SportsItem:“+getCode()+”:“+“RM”+最终价格;
退货项目;
}
@凌驾
公共布尔值isOnBudget(){
返回getCurrentPrice()<150.00;
}
}
分类零售商品
{
私人物品[]物品清单;
公共零售(商品)
{
itemList=新项目[10];
itemList[0]=新衣物项目(10001,85,真);
itemList[1]=新衣物项目(10002150,假);
itemList[2]=新衣物项目(10003168,真);
itemList[3]=新衣物项目(10004,43,真);
itemList[4]=新衣物项目(10005162,假);
itemList[5]=新运动项目(10006178,假);
itemList[6]=新的运动项目(10007,80,真);
itemList[7]=新的运动项目(10008191,假);
itemList[8]=新的运动项目(10009,45,真);
itemList[9]=新的运动项目(10010121,真);
}
公共无效打印项(){
对于(int i=0;i0&&itemList[i].isOnBudget())
{
System.out.println(itemList[i].printbughtgst());
}
}
}
}
类TestRetailItem{
公共静态void main(字符串[]args){
Retail_Item ret=新的Retail_Item();
ret.printItem();
}
}
您需要阅读本文并展示一些努力。请编辑问题并缩小问题范围。
public class Retail_Item 
{
    private Item[] itemList;
    public Retail_Item()
    {
        itemList = new Item[10];
        itemList[0] = new ClothingItem(10001,85,true);
        itemList[1] = new ClothingItem(10002,150,false);
        itemList[2] = new ClothingItem(10003,168,true);
        itemList[3] = new ClothingItem(10004,43,true);
        itemList[4] = new ClothingItem(10005,162,false);
        itemList[5] = new SportsItem(10006,178,false);
        itemList[6] = new SportsItem(10007,80,true);
        itemList[7] = new SportsItem(10008,191,false);
        itemList[8] = new SportsItem(10009,45,true);
        itemList[9] = new SportsItem(10010,121,true);
    }
    public  void printItem()
    {
        for(int i =0 ;i<itemList.length;i++)
        {

            if(itemList[i].getIsOnGST()==true && itemList[i].printBudgetGST(itemList).length()>0)
            {

                System.out.println(itemList[i].printBudgetGST(itemList));
            }
        }
    }
}


public class TestRetailItem {

    public static void main(String[] args)
    {
        Retail_Item ret = new Retail_Item();
        ret.printItem();

    }

}
public abstract class Item {
    private int code;
    private double price;
    private boolean isOnGST;

    public Item()
    {

    }
    public Item(int code,double price,boolean isOnGST)
    {
        this.code = code;
        this.price = price;
        this.isOnGST = isOnGST;
    }
    public void setGST(boolean isgst)
    {
        this.isOnGST = isgst;
    }
    public int getCode()
    {
        return code;
    }
    public boolean getIsOnGST()
    {
        return isOnGST;
    }
    public double getCurrentPrice()
    {
        return price;
    }
    public String toString() {
        return "Item [code=" + code + ", price=" + price + ", isOnGST=" + isOnGST + "]";
    }
    public abstract String printBudgetGST();
    public abstract boolean isOnBudget();
}

class ClothingItem extends Item {
    public ClothingItem() {

    }

    public ClothingItem(int code, double price, boolean isOnGST) {
    super(code, price, isOnGST);
    }

    @Override
    public String printBudgetGST() {
    String stringitem = "";
    double finalprice = (0.06 * getCurrentPrice()) + getCurrentPrice();
    stringitem = stringitem + " " + "ClothingItem : " + getCode() + ":" + "RM" + finalprice;

    return stringitem;
    }

    @Override
    public boolean isOnBudget() {
    return getCurrentPrice() < 100.00;
    }
}

class SportsItem extends Item {
    public SportsItem() {
    }

    public SportsItem(int code, double price, boolean isOnGST) {
    super(code, price, isOnGST);
    }

    public String printBudgetGST() {
    String stringitem = "";
    double finalprice = (0.06 * getCurrentPrice()) + getCurrentPrice();
    stringitem = stringitem + "SportsItem : " + getCode() + ":" + "RM" + finalprice;
    return stringitem;
    }

    @Override
    public boolean isOnBudget() {
    return getCurrentPrice() < 150.00;
    }
}

class Retail_Item
{
    private Item[] itemList;

    public Retail_Item()
    {
    itemList = new Item[10];
    itemList[0] = new ClothingItem(10001,85,true);
    itemList[1] = new ClothingItem(10002,150,false);
    itemList[2] = new ClothingItem(10003,168,true);
    itemList[3] = new ClothingItem(10004,43,true);
    itemList[4] = new ClothingItem(10005,162,false);
    itemList[5] = new SportsItem(10006,178,false);
    itemList[6] = new SportsItem(10007,80,true);
    itemList[7] = new SportsItem(10008,191,false);
    itemList[8] = new SportsItem(10009,45,true);
    itemList[9] = new SportsItem(10010,121,true);
    }

    public  void printItem() {
    for(int i =0 ;i<itemList.length;i++) {
        if(itemList[i].getIsOnGST()==true && itemList[i].printBudgetGST().length()>0 && itemList[i].isOnBudget())
        {

        System.out.println(itemList[i].printBudgetGST());
        }
    }
    }
}

class TestRetailItem {
    public static void main(String[] args) {
    Retail_Item ret = new Retail_Item();
    ret.printItem();
    }
}