方法和继承麻烦

时间:2016-02-09 01:48:01

标签: inheritance methods

我正在使用SalesPersonHourlyEmployee继承的继承程序。我应该制作getWeeklySalary方法,但不断出错。任何人都可以告诉我我做错了什么。 这是员工:

import java.io.*;
import java.util.*;
import java.lang.*;
import java.text.*;

 public abstract class Employee
 {
 private String name;
 private String idnum;
 private String datehired;
 private double weeklysalary;

public Employee()
{
    this.name = " ";
    this.idnum = " ";
    this.weeklysalary = 0;
    this.datehired= " ";
}
public Employee ( String name,String idnum, String datehired )
{
    this.name =name;
    this.idnum =idnum;
    this.datehired = datehired;
}
public void setName( String name){
    this.name = name;
}


public void setId( String idnum ){
    this.idnum = idnum;
}
public void setDate( String datehired ){
    this.datehired = datehired;
}
public void setSalary( double weeklysalary ){
    this.weeklysalary = weeklysalary;
}


public String getName()
{return name;}

public String getId()
{return idnum;}

public String getDate()
{return datehired;}

public double getSalary()
{return weeklysalary;}

public boolean equals(Employee otherObject)
  {
      if(otherObject == null)
         return false;
      if(otherObject == this)
         return true;
      if(this.getId() != otherObject.getId())
         return false;
     if (this.getId()==otherObject.getId())
         return true;
     else
     return false;
}

public String toString()
  {

      return "Name: " + name + " ID: " + idnum +
       "Date Hired: " + datehired ;
  }

 public abstract double getWeeklySalary();



}

这是HourlyEmployee

import java.io.*;
import java.util.*;
import java.lang.*;
import java.text.*;


public class HourlyEmployee extends Employee
 {


private double hourlyrate;
private double hoursworked;


public HourlyEmployee()
{
    super();
    this.hourlyrate = 0;
    this.hoursworked = 0;

}
public HourlyEmployee( String name, String idnum, String datehired, double hourlyrate, double hoursworked)
{   super(name, idnum, datehired);
    this.hourlyrate = hourlyrate;
this.hoursworked = hoursworked;

}
public void setRate( double hourlyrate){
    this.hourlyrate = hourlyrate;
}
public void setHours( double hoursworked){
    this.hoursworked= hoursworked;
}



public double getRate()
{return hourlyrate;}

public double getHours()
{return hoursworked;}

public double getWeeklySalary() {
    if (hoursworked >35){
        double week = hourlyrate * 2 *  hoursworked;
        return week;

}
return hourlyrate * hoursworked;
}

public String toString()
  {String result =  super.toString();
     result += "Rate of pay:" + hourlyrate + "Hours worked:" + hoursworked ;
   return result;}
}

这是SalesPerson:

import java.io.*;
import java.util.*;
import java.lang.*;
import java.text.*;


public class SalesPerson extends HourlyEmployee
 {


private double rateofcommission;
private double totalsales;



public SalesPerson()
{
    super();
    this.rateofcommission = 0;
    this.totalsales = 0;

}
public SalesPerson( String name, String idnum, String datehired, double hourlyrate, double hoursworked, double rateofcommission)
{   super(name, idnum, datehired, hourlyrate, hoursworked);
    this.rateofcommission = rateofcommission;


    }



public void setCommission( double rateofcommissions){
    this.rateofcommission = rateofcommission;
}

public double getCommission()
{return rateofcommission;}

public double getTotal()
{return totalsales;}


 public void addSale(double amt)
   { this.totalsales += amt;}

public void ClearSales(){
this.totalsales=0;}

public double getWeeklySalary() {
    if (hoursworked >35){
        double week = hourlyrate * 2 *  hoursworked + rateofcommission * totalsale;
        return week;

}
return hourlyrate * hoursworked + rateofcommission * totalsale;
}

}

0 个答案:

没有答案