【发布时间】:2015-06-21 02:45:59
【问题描述】:
我正在尝试将补货费放入 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>
【问题讨论】:
-
您可以通过movieTitle中的方法
super.restockFee()调用DVD中的方法restockFee()。