我试图将补货费用放在DVD子类movieTitle中。编辑我的代码应该是它的方式,但它不起作用。 DVD是父类,movieTitle是子类。我试图从子类到父类获得方法补货费...请帮忙。我不知道如何从子类调用该方法在需要打印出来的父类中可用。我找到了解释子类和继承的地方,但没有找到如何实现子类的功能(方法)。
线程“main”中的异常java.lang.RuntimeException:无法编译的源代码 - 错误的sym类型:inventoryprogrampart3.DVD.movieTitle.restockFee 在inventoryprogrampart3.DVD.toString(DVD.java:126)
说明:
通过创建产品类的子类来修改库存程序,该子类使用您选择的产品的另一个独特功能(例如,对于DVD子类,您可以使用电影标题)。在子类中,创建一个方法来计算产品库存的值,该方法的名称与先前为产品类创建的方法相同。子类方法还应该为该产品的库存价值增加5%的重新进货费用。 修改输出以显示您选择的此附加功能和重新进货费用。
<code>
import java.util.Arrays;
public class InventoryProgramPart3 {
public static void main(String[] args) {
DVD[] myDVD = new DVD[5]; // create array of DVD's
// inventory of DVD's
DVD p1 = new DVD ( 1, "Fast and Furious 6", 8, 5 );
DVD p2 = new DVD ( 2, "The Matrix Reloaded", 8, 5 );
DVD p3 = new DVD ( 3, "In Pursuit of Happiness", 8, 5 );
DVD p4 = new DVD ( 4, "Darknet", 8, 5 );
DVD p5 = new DVD ( 5, "Goonies", 8, 5 );
myDVD[0] = p1;
myDVD[1] = p2;
myDVD[2] = p3;
myDVD[3] = p4;
myDVD[4] = p5;
double total = 0.0;
for (int i = 0; i < 5; i++)
{
total += myDVD[i].getInvValue();
}
// Display the total value of the inventory on the screen
//sorting DVD's alphabetically
Arrays.sort(myDVD);
for(DVD s: myDVD)
{
System.out.println(s);
}
System.out.println();
System.out.printf("Total value of the entire inventory is: $ %.2f", total);
System.out.println();
} // end main method
}//end class
package inventoryprogrampart3;
@SuppressWarnings("rawtypes")
class DVD implements Comparable
{
private String thisTitle;
private double thisStock;
private double thisPrice;
private double thisItem;
public DVD( double item, String title, double stock, double price )
{
thisItem = item;
thisTitle = title;
thisStock = stock;
thisPrice = price;
}// end constuctor
// Getters and setters
public void setTitle(String title)
{
thisTitle = title;
}//end method setTitle
//return Title
public String getTitle()
{
return thisTitle;
}//end method getTitle
//set Stock
public void setStock(double stock)
{
thisStock = stock;
}//end method setStock
//return Stock
public double getStock()
{
return thisStock;
}//end method get Stock
public void setPrice(double price)
{
thisPrice = price;
}//end method setPrice
//return Price
public double getPrice()
{
return thisPrice;
}//end method getPrice
public void setItem(double item)
{
thisItem = item;
}//end method setItem
//return Item
public double getItem()
{
return thisItem;
}//end method getItem
//calculate the inventory value
public double value()
{
return thisPrice * thisStock;
}//end method value
public double getInvValue()
{
double invValue = 0;
for(int i = 0; i < 5; i++)
{
invValue = value();
}
return invValue;
}
@Override
public int compareTo (Object o)
{
DVD s = (DVD)o;
return thisTitle.compareTo(s.getTitle());
}
@Override
public String toString() //
{
System.out.println();
**return "Item number is: \t\t" + thisItem + "\nProducts Title is: \t\t"+
thisTitle+"\nPrice: \t\t\t\t$" + thisPrice + "\nQuantity: \t\t\t"
+ thisStock + "\nValue: \t\t\t\t$" + value() + "\nRestock fee:
\t\t\t$" + restockFee() ;** <-- error here..........
restock method is in the subclass (movieTitle), and do not not how to
implement it here in the parent class.
} //end main method
} // End DVD class
package inventoryprogrampart3;
class movieTitle extends DVD {
private double thisRestockFee;// Restock fee to add to the inventory value
public movieTitle(double item, String title, double stock,
double price, double restockFee)
{
super(item, title, stock, price);
thisRestockFee = restockFee;
}
public double restockFee() {
return super.getPrice() * .05;
}// end method value
} // End movieTitle class
</code>
答案 0 :(得分:0)
制作DVD摘要并将restockFee更改为这样
public abstract double restockFee();
现在movieTitle看起来应该是这样的
class movieTitle extends DVD {
private double restockFee1;// Restock fee to add to the inventory value
public movieTitle(double item, String title, double stock,
double price, double restockFee) {
super(item, title, stock, price);
this.restockFee1 = restockFee;
}
@Override
public double restockFee() {
return super.getPrice() * restockFee1;
}// end method value
} // End movieTitle class
由于super还没有实现restockFee,因此它将调用child的实现。并且在测试中使用movieTitle。