Java 我如何填写一个主要类的空白,饮料/饮料盒/饮料饮料杯/等等

Java 我如何填写一个主要类的空白,饮料/饮料盒/饮料饮料杯/等等,java,switch-statement,main,Java,Switch Statement,Main,我设置了除main之外的所有类,这就是我需要做的: case 'A': //Add Drink System.out.print("Please enter a drink information to add:\n"); inputInfo = scan.nextLine().trim(); /*****************************************************************************

我设置了除main之外的所有类,这就是我需要做的:

case 'A':   //Add Drink
           System.out.print("Please enter a drink information to add:\n");
           inputInfo = scan.nextLine().trim();
/***********************************************************************************
***  ADD your code here to create an object of one of child classes of Drink class
***  and add it to the drinkList
***********************************************************************************/
           break;
         case 'C':   //Compute Total Prices
/***********************************************************************************
***  ADD your code here to compute the total price for all drinks in the list.
***********************************************************************************/
           for(int i = 0 ; i < drinkList.length(); i++) {
               drinkList[i]
           }
           System.out.print("total prices computed\n");
           break;
         case 'D':   //Search for Drink
           System.out.print("Please enter a drinkID to search:\n");
           inputInfo = scan.nextLine().trim();
/***********************************************************************************
***  ADD your code here to search a given drinkID. If found, set "found" true,
***  and set "found" false otherwise.
***********************************************************************************/
            if (found == true)
             System.out.print("drink found\n");
            else
             System.out.print("drink not found\n");
           break;
这是drinkInBox和drinkInCylinder中的方法:

public void computeTotalPrice()
{
    volume = (int)(Math.PI*(radius * radius * height));

    totalPrice = volume * unitPrice;
}
我需要做什么来填补这些空白?我认为计算总价应该是这样的:

for(int i = 0 ; i < drinkList.length(); i++) {
               drinkList[i] //I don't know how to call the computeTotalPrice method for any particular index of drinkList, I can't say .drinkInCylinder or drinkInBox because there isn't a way to tell which object type it is
for(int i=0;i
您可以这样做

for(int i = 0 ; i < drinkList.length(); i++) {
    if(drinkList[i] instanceof DrinkInBox)
        (DrinkInBox)drinkList[i].computeTotalPrice();
    else if(drinkList[i] instanceof DrinkInCylinder)
        (DrinkInCylinder)drinkList[i].computeTotalPrice();
}
for(int i=0;i
但是,这有点多余,而且很明显,您可以拥有一个超级类,比如说
Drink
。您应该在该类上定义
computeTotalPrice
DrinkInBox
drinkincolumne
都将继承
Drink


顺便说一句,按照Java命名约定,类应该以大写字母开头。

你的
drinkList
是什么的
列表
?程序会询问你是想添加饮料、计算价格、搜索饮料还是列出它们。当你键入它们时,drinkList会存储每一个。它们必须以这种格式编写:对于一个赛林人der drink,shape/drinkId/unitPrice/radius/height For a box drink,shape/drinkId/unitPrice/height/width/depthYes!drink是超类,DrinkInBox扩展了drink,DrinkInCylinder也扩展了drink。很抱歉,我忘了大写。如果我们有一个超类,但它没有computeTotalPrice方法,它是否继承了这两个超类的方法子类中的一个?将一个放在饮料类中的问题是,每个类的方法不同,一个通过圆柱体逻辑计算体积,另一个作为hwl,使用的变量仅在其中任何一个类中。您在超类中定义它,但在子类中重写它。当您从超类调用该方法时,他知道要执行哪一个。定义它是指:public abstract void computeTotalPrice();如果是,那么我是否仍然需要指定要执行哪一个?如果我只使用(Drink)drinkList[I]。computeTotalPrice();它如何知道它是圆柱体还是长方体?
for(int i = 0 ; i < drinkList.length(); i++) {
    if(drinkList[i] instanceof DrinkInBox)
        (DrinkInBox)drinkList[i].computeTotalPrice();
    else if(drinkList[i] instanceof DrinkInCylinder)
        (DrinkInCylinder)drinkList[i].computeTotalPrice();
}