无法让我的子类将数据发送回main方法进行输出 import java.text.DecimalFormat;//为了合适的货币 导入java.util.array; 导入java.util.Comparator; 公共类目录{ 公共静态void main(字符串参数[]) { //启动软件标题数组 软件[]aSoftware=新软件[4]; aSoftware[0]=新软件(“命令与征服”,6,29.9910122); aSoftware[1]=新软件(“爱丽丝梦游仙境”,1,10.9910233); aSoftware[2]=新软件(“厄运”,1,10.9910344); aSoftware[3]=新软件(“行尸走肉”,6,9.9910455); //设置货币格式 十进制货币=新的十进制货币($0.00); //按软件名称的顺序排序 sort(aSoftware,新的Comparator(){ 公共整数比较(软件s1、软件s2){ 返回s1.getSoftwareTitle().compareTo(s2.getSoftwareTitle()); } }); //显示软件标题、单位数量、成本、项目编号和总库存 对于(int i=0;i
我的程序运行得很好,除了我无法从我的Project 3子类中取出我的系统来提取getYear方法和getRestockingFee nethod之外。我可以在正确的方向上使用轻推,我必须错过一些疯狂的东西。当给Java编译器一个无法让我的子类将数据发送回main方法进行输出 import java.text.DecimalFormat;//为了合适的货币 导入java.util.array; 导入java.util.Comparator; 公共类目录{ 公共静态void main(字符串参数[]) { //启动软件标题数组 软件[]aSoftware=新软件[4]; aSoftware[0]=新软件(“命令与征服”,6,29.9910122); aSoftware[1]=新软件(“爱丽丝梦游仙境”,1,10.9910233); aSoftware[2]=新软件(“厄运”,1,10.9910344); aSoftware[3]=新软件(“行尸走肉”,6,9.9910455); //设置货币格式 十进制货币=新的十进制货币($0.00); //按软件名称的顺序排序 sort(aSoftware,新的Comparator(){ 公共整数比较(软件s1、软件s2){ 返回s1.getSoftwareTitle().compareTo(s2.getSoftwareTitle()); } }); //显示软件标题、单位数量、成本、项目编号和总库存 对于(int i=0;i,java,subclass,Java,Subclass,我的程序运行得很好,除了我无法从我的Project 3子类中取出我的系统来提取getYear方法和getRestockingFee nethod之外。我可以在正确的方向上使用轻推,我必须错过一些疯狂的东西。当给Java编译器一个软件对象时,它无法知道它实际上是一个项目3。在现实生活中,如果我把一个史密斯奶奶的苹果放在一个盒子里,我告诉你盒子里有一个苹果,你能告诉我它是否是绿色的吗?Java也有同样的问题 有3种可能的解决方案: 构造对象时,将其存储在Project3变量中(并确保构造的类型是Pr
软件
对象时,它无法知道它实际上是一个项目3
。在现实生活中,如果我把一个史密斯奶奶的苹果放在一个盒子里,我告诉你盒子里有一个苹果,你能告诉我它是否是绿色的吗?Java也有同样的问题
有3种可能的解决方案:
Project3
变量中(并确保构造的类型是Project3
)((Project3)mySoftware)。getYear()
getYear
定义一个含义。这就像告诉你“嘿,假设所有的苹果都不是绿色的(isGreen()
returnsfalse
)”……但是如果我知道它是一个Granny Smith子类apple,isGreen
现在返回true
您对解决方案的选择取决于您希望系统执行的操作。错误消息是类型软件的方法getYear()未定义,类型软件的方法getRestockingFee()未定义,位于Inventory.main(Inventory.java:47)类getYear()doe
import java.text.DecimalFormat; // For proper currency
import java.util.Arrays;
import java.util.Comparator;
public class Inventory {
public static void main( String args[] )
{
// Start array of software titles
Software[] aSoftware = new Software[4];
aSoftware[0]= new Software("Command and Conquer ", 6, 29.99, 10122);
aSoftware[1]= new Software("Alice in Wonderland", 1, 10.99,10233);
aSoftware[2]= new Software("Doom", 1, 10.99, 10344);
aSoftware[3]= new Software("Walking Dead", 6, 9.99, 10455);
//Set currency format
DecimalFormat money = new DecimalFormat("$0.00");
// Sort in order of Software Name
Arrays.sort(aSoftware, new Comparator<Software>() {
public int compare(Software s1, Software s2) {
return s1.getSoftwareTitle().compareTo(s2.getSoftwareTitle());
}
});
// Display software title, number of units, cost, item number and total inventory
for (int i = 0; i < aSoftware.length; i++){
System.out.println("Software Title is "+ aSoftware[i].getSoftwareTitle() );
System.out.println("The number of units in stock is "+ aSoftware[i].getSoftwareStock() );
System.out.println("The price of the Software Title is "+ (money.format(aSoftware[i].getSoftwarePrice() )));
System.out.println( "The item number is "+ aSoftware[i].getSoftwareNum());
System.out.println( "The year of copyright is "+ aSoftware[i].getYear());
System.out.println( "The restocking fee is "+ aSoftware[i].getRestockingFee());
System.out.println( "The value of the Software Inventory is "+ (money.format(aSoftware[i].Softwarevalue() )));
System.out.println();
}
//output total inventory value
double total = 0.0;
for (int i = 0; i < 3; i++){
total += aSoftware[i].getCalculateInventory();
}
System.out.printf("Total Value of Software Inventory is: \t$%.2f\n", total);
//end output total inventory value
}
} //end main
public class Software
{
// Declare variables
String SoftwareTitle;
int SoftwareStock;
double SoftwarePrice;
int SoftwareNum;
double CalculateInventory;
double SoftwareValue;
double value;
Software( String softtitle, int softstock, double softprice, int softitemnum )
{
// Create object constructor
SoftwareTitle = softtitle;
SoftwareStock = softstock;
SoftwarePrice = softprice;
SoftwareNum = softitemnum;
}
// Set Software Title
public void setSoftwareTitle( String softtitle )
{
SoftwareTitle = softtitle;
}
// Return Software Title
public String getSoftwareTitle()
{
return SoftwareTitle;
}
// Set software inventory
public void setSoftwareStock( int softstock)
{
SoftwareStock = softstock;
}
// Return software inventory
public int getSoftwareStock()
{
return SoftwareStock;
}
// Set software price
public void setSoftwarePrice( double softprice )
{
SoftwarePrice = softprice;
}
// Return software price
public double getSoftwarePrice()
{
return SoftwarePrice;
}
// Set item number
public void setSoftwareNum( int softitemnum )
{
SoftwareNum = softitemnum;
} //
//return software item number
public int getSoftwareNum()
{
return SoftwareNum;
} //
// calculate inventory value
public double Softwarevalue()
{
return SoftwarePrice * SoftwareStock;
}
public void setCalculateInventory (double value){
this.CalculateInventory = value;
}
public double getCalculateInventory(){
double value = 0;
for(int i = 0; i < 3; i++){
value = Softwarevalue();
}
return value;
}
}//end method value
//
public class Project3 extends Software
{
// Unique feature
private int year = 2012;
// Default Constructor
public Project3(String softtitle, int softstock, double softprice,int softitemnum, int year) {
super(softtitle, softstock, softprice, softitemnum);
this.year = year;
}
// Retuen Year
public int getYear()
{
return year;
}
// Set year
public void setYear(int year)
{
this.year = year;
}
// Calculate 5% restocking fee
public double getRestockingFee()
{
return getSoftwarePrice() * getSoftwareStock() * 0.05;
}
public double getCalculateInventory()
{
double value = 0;
for(int i = 0; i < 3; i++){
value = Softwarevalue();
}
return value;
}
}