【问题标题】:Static ArrayLists and Casting Classes静态 ArrayLists 和转换类
【发布时间】:2018-05-12 22:38:44
【问题描述】:

我在尝试使用多态性的 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

我附上下面的代码,我的问题是:

  1. 如何从外部方法访问主方法中的数组列表?在项目的描述中,它说我使用静态数组列表,但是当我尝试这样做时,它给了我错误。

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());
    }
}


}

【问题讨论】:

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

标签: java class arraylist polymorphism


【解决方案1】:

您的ArrayLists 应该是类变量,因此必须在main() 之外声明它们,以便类的其余部分可以访问它们。由于您处于静态上下文中,因此必须将它们声明为 static ArrayList&lt;...&gt;。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多