很难在主要方法中调用方法。一直说它找不到符号 - 方法addSales(double)
我正在做一个编程项目,因为我们刚刚学习了继承,它有3个超类和3个子类。我遇到了一个子课和超级课的问题。他们被命名为Hourly and Commission。佣金每小时延长一次。我觉得我已经正确地编写了名为addSales的方法,但是当我在main中调用它时它说它找不到方法。我在这里错过了什么吗?任何帮助将不胜感激。
佣金等级:
public class Commission extends Hourly
{
private double totalSales, commission;
public Commission(String eName, String eAddress, String ePhone,
String socSecNumber, double rate, double commissionRate)
{
super(eName, eAddress, ePhone, socSecNumber, rate);
totalSales = 0.0;
commission = commissionRate;
}
public double pay()
{
double payment = super.pay();
payment = (payment + (commission * totalSales));
return payment;
}
public String toString()
{
String result = super.toString();
result += "Total Sales: " + totalSales;
return result;
}
public void addSales(double totalS)
{
totalSales = totalSales + totalS;
}
}
每小时课程:
public class Hourly extends Employee
{
private int hoursWorked;
//-----------------------------------------------------------------
// Sets up this hourly employee using the specified information.
//-----------------------------------------------------------------
public Hourly (String eName, String eAddress, String ePhone,
String socSecNumber, double rate)
{
super (eName, eAddress, ePhone, socSecNumber, rate);
hoursWorked = 0;
}
//-----------------------------------------------------------------
// Adds the specified number of hours to this employee's
// accumulated hours.
//-----------------------------------------------------------------
public void addHours (int moreHours)
{
hoursWorked += moreHours;
}
//-----------------------------------------------------------------
// Computes and returns the pay for this hourly employee.
//-----------------------------------------------------------------
public double pay()
{
double payment = payRate * hoursWorked;
hoursWorked = 0;
return payment;
}
//-----------------------------------------------------------------
// Returns information about this hourly employee as a string.
//-----------------------------------------------------------------
public String toString()
{
String result = super.toString();
result += "\nCurrent hours: " + hoursWorked;
return result;
}
}
主:
public class Firm
{
//--------------------------------------------------------------
// Creates a staff of employees for a firm and pays them.
//--------------------------------------------------------------
public static void main (String[] args)
{
Staff personnel = new Staff(8);
Executive staff0 = new Executive ("Sam", "123 Main Line", "555-0469", "123-45- 6789", 2423.07);
StaffMember staff1 = new Employee ("Carla", "456 Off Line", "555-0101", "987-65-4321", 1246.15);
StaffMember staff2 = new Employee ("Woody", "789 Off Rocker", "555-0000", "010-20-3040", 1169.23);
Hourly staff3 = new Hourly ("Diane", "678 Fifth Ave.", "555-0690", "958-47-3625", 10.55);
Hourly staff6 = new Commission("Marcus Gendron", "66 Highland St.", "272-9555", "123-62-5678", 6.25, 0.20);
Hourly staff7 = new Commission("Joe Dangerous", "55 dude Court", "555-1267", "777-66-5555", 9.75, 0.15);
StaffMember staff4 = new Volunteer ("Norm", "987 Suds Blvd.", "555-8374") ;
StaffMember staff5 = new Volunteer ("Cliff", "321 Duds Lane", "555-7282");
personnel.addStaff(staff0);
personnel.addStaff(staff1);
personnel.addStaff(staff2);
personnel.addStaff(staff3);
personnel.addStaff(staff4);
personnel.addStaff(staff5);
personnel.addStaff(staff6);
personnel.addStaff(staff7);
staff6.addHours(35);
staff6.addSales(400.0);
//error is being shown here ^^^^
staff7.addHours(40);
staff7.addSales(950.00);
staff0.awardBonus (500.00);
staff3.addHours (40);
personnel.payday();
}
}
答案 0 :(得分:1)
staff6
和staff7
都是Hourly
的实例。 Hourly
类没有附加特定方法。
您必须将它们声明为Commission
的具体实例。
答案 1 :(得分:0)
staff6
被声明为Hourly
,即使它实际上是Commission
,您也试图访问超类'方法。
将其声明为Commission
。
答案 2 :(得分:0)
我在下面看到了对象创建:
Hourly staff6 = new Commission("Marcus Gendron", "66 Highland St.", "272-9555", "123-62-5678", 6.25, 0.20);
addSales方法在子类委员会中。但是staff6的类型是Hourly,它是父类,而Hourly类没有这个方法。你没有看到方法addsales。
有关更详细的理解,请尝试以下行(编译正常)。看看你得到的所有方法
Object staff6 = new Commission("Marcus Gendron", "66 Highland St.", "272-9555", "123-62-5678", 6.25, 0.20);
答案 3 :(得分:0)
此处您只能调用每小时课程中出现的方法。
因为您现在将Hourly类引用为每小时类中不存在的addSales,所以它无法访问。
Hourly staff6 = new Commission("Marcus Gendron", "66 Highland St.", "272-9555", "123-62-5678", 6.25, 0.20);
在上面的代码中,Commission类对象引用了Hourly类。所以委员会对象只允许访问那些属于Hourly类的方法(因为它们是继承的),并且在委托类中重写了每小时类的方法。