Java 静态ArrayList和Casting类

Java 静态ArrayList和Casting类,java,class,arraylist,polymorphism,Java,Class,Arraylist,Polymorphism,我在Java项目中遇到了一些问题,我试图使用多态性。我有以下的类,也可以共享指令,类作为代码和UML图。提前谢谢 GroceryItem abstract Class Perishable Class NonPerishable Class meat Class extends Perishables-----CannedSoup Class extends NonPerishables fruit Class

我在Java项目中遇到了一些问题,我试图使用多态性。我有以下的类,也可以共享指令,类作为代码和UML图。提前谢谢

                   GroceryItem abstract Class

          Perishable Class             NonPerishable Class

meat Class extends Perishables-----CannedSoup Class extends NonPerishables
fruit Class extends Perishables----Cereal Class extends NonPerishables
我附上下面的代码,我的问题是:

  • 如何从外部方法访问主方法中的数组列表?在项目的描述中,它说我使用静态数组列表,但当我尝试这样做时,它给了我错误
  • 2.我如何知道我使用的方法中的易腐物品是水果还是肉

    3.为什么要求我按照说明对项目进行排序并将其添加到数组列表中?这意味着什么

    import java.util.ArrayList;
    
    public class ShoppingCart {
    public static void main(String args[]) {//main method
    
        ArrayList<Perishable> perishables = new ArrayList<Perishable>();
        ArrayList<NonPerishable> nonPerishables = new ArrayList<NonPerishable>();
    
        Cereal cereal = new Cereal();
        cereal.setBrandName("Kellogs");
        cereal.setManufacturer("Sereal Manufacturing Co.");
        cereal.setUnitPrice(12.79);
        //nonPerishables.add(cereal);//need to use sortItems method according to the instruction
    
        CannedSoup soup = new CannedSoup();
        soup.setSoupMaker("TastySoups Co.");
        soup.setDescription("Mushroom soup with natural flavors...");
        soup.setUnitPrice(2.39);
        //nonPerishables.add(soup); //need to use sortItems method according to the instruction
    
    
        Meat meat = new Meat();
        meat.setWeightInPounds(1.29);
        meat.setDescription("T-Bone Steak");
        meat.setPricePerPound(10.89);
        //perishables.add(meat); //need to use sortItems method according to the instruction
    
        Fruit fruit = new Fruit();
        fruit.setDescription("Bananas imported from Brazil.");
        fruit.setPricePerBag(6.99);
        //perishables.add(fruit); //need to use sortItems method according to the instruction
    
        System.out.println("Price of " + meat.getDescription() + " is:" + meat.getTotalPrice());
        System.out.println("Price of " + fruit.getDescription() + " is:" + fruit.getTotalPrice());
    
        sortItems(cereal);//adding the object to list through this method
        sortItems(soup);
        sortItems(meat);
        sortItems(fruit);
    
        displayItems();//print all the items 
    
    }
    
    
    /*
     * sortItems: This function will be to take the item passed in and sort and add the item into one of the of Perishable 
     * or NonPerishable ArrayList. Use casting and/or the instanceof keyword to determine which list the item should be added to.
     * I dont know what did they mean by sort items...
     */
    static void sortItems(GroceryItem item){//This method needs to take 
        if(item instanceof Perishable) 
            perishables.add(item);//error here because I dont have access to arraylist
    
        if(item instanceof NonPerishable) 
            nonPerishables.add(item);//error here because I dont have access to arraylist
    }
    
    
    static void displayItems() {//display all the items from arraylist
        //if(item instanceof Perishable)
    
        System.out.println("Here is the list of your Shopping Cart:\n--------------------------");
        System.out.println("Perishables:\n----");
    
            for(Perishable item : perishables) //print all the perishables-How can I acces to arraylist in main method
                System.out.println(item.getDescription() + ", total price:" + item.getTotalPrice());
    
        System.out.println("NonPerishables:\n----");
    
        for(NonPerishable item : nonPerishables) {//how do I access to array list at main method
            if(item.getSoupMaker)//how do I check if it is soup or not without adding extra method to class
                System.out.println(item.getDescription + " " + item.getSoupMaker() + ", unitPrice:" + item.getUnitPrice() + 
                                            " totalPrice:" + item.getTotalPrice());
            else//if it is not soup it should be cereal
                System.out.println(item.getBrand() + " " + item.getManufacturer() + ", unitPrice:" + item.getUnitPrice() +
                                        " totalPrice:" + item.getTotalPrice());
        }
    }
    
    
    }
    
    import java.util.ArrayList;
    公共类购物车{
    公共静态void main(字符串args[]){//main方法
    ArrayList易腐物品=新ArrayList();
    ArrayList nonPerishables=新的ArrayList();
    谷物=新谷物();
    谷类食品。setBrandName(“Kellogs”);
    谷物制造商(“Sereal制造公司”);
    谷物.设定单价(12.79);
    //不易腐烂。添加(谷物);//需要根据说明使用sortItems方法
    罐头汤=新罐头汤();
    汤。setSoupMaker(“TastySoups Co.”;
    汤.setDescription(“天然风味蘑菇汤…”);
    汤.设定单价(2.39);;
    //不易变质。添加(汤);//需要根据说明使用sortItems方法
    肉=新肉();
    肉.整磅重(1.29);
    肉。肉质描述(“T骨牛排”);
    肉.每磅设定价格(10.89);
    //易腐食品。添加(肉);//需要根据说明使用sortItems方法
    水果=新水果();
    水果.setDescription(“从巴西进口的香蕉”);
    水果.每袋售价(6.99);
    //易腐食品。添加(水果);//需要根据说明使用sortItems方法
    System.out.println(“+meat.getDescription()+”的价格是:“+meat.getTotalPrice());
    System.out.println(“+fruit.getDescription()+”的价格是:“+fruit.getTotalPrice());
    sortItems(谷物);//通过此方法将对象添加到列表中
    菌痢(汤);
    菌痢(肉);
    菌核(果实);
    displayItems();//打印所有项目
    }
    /*
    *sortItems:此函数将获取传入的项目,并对其进行排序,然后将其添加到易腐物品中
    *或不易损坏的ArrayList。使用强制转换和/或instanceof关键字确定应将项目添加到哪个列表。
    *我不知道他们所说的分类项目是什么意思。。。
    */
    静态无效sortItems(GroceryItem项){//此方法需要
    if(易腐物品实例)
    易腐物品。添加(物品);//此处出错,因为我无权访问arraylist
    if(不易损坏的项目实例)
    不易损坏。添加(项);//此处出错,因为我无权访问arraylist
    }
    静态void displayItems(){//显示arraylist中的所有项
    //if(易腐物品实例)
    System.out.println(“这是您的购物车的列表:\n---------------------------------------------------”);
    System.out.println(“易腐物品:\n--”);
    for(易腐物品:易腐物品)//打印所有易腐物品如何在main方法中访问arraylist
    System.out.println(item.getDescription()+”,总价:“+item.getTotalPrice());
    System.out.println(“不易损坏:\n--”);
    for(nonperiphable item:nonperiphables){//如何在main方法中访问数组列表
    if(item.getSoupMaker)//如何在不向类中添加额外方法的情况下检查它是否为汤
    System.out.println(item.getDescription+“”+item.getSoupMaker()+”,单价:“+item.getUnitPrice()+
    “总价:”+item.getTotalPrice());
    否则//如果不是汤,就应该是麦片粥
    System.out.println(item.getBrand()+“”+item.getManufacturer()+”,单价:“+item.getUnitPrice()+
    “总价:”+item.getTotalPrice());
    }
    }
    }
    
    您的
    ArrayLists
    是类变量,因此它们必须在
    main()
    之外声明,以便类的其余部分可以访问它们。由于您处于静态上下文中,因此必须将它们声明为
    静态ArrayList

    您至少要问三个不同的问题。我投票结束这个问题,因为它太宽泛了。我也添加了描述,你可以在@mulliganacile结尾处找到。我只需要知道,当我试图将数组列表公开为静态时,为什么会出现错误,以及如何检测对象的类型?提前谢谢。