项目有接口...我认为这是netbean的错误

时间:2015-03-26 13:03:06

标签: java interface inherited

我有一个看起来像这样的项目:

public interface Payable{
    double getPaymentAmount(); // calculate payment; no implementation
} // end interface Payable

然后我有一个像这样的抽象类:

    public abstract class Employee implements Payable
    {
       private String firstName;
       private String lastName;
       private String socialSecurityNumber;
       // three-argument constructor
       public Employee( String first, String last, String ssn )
       {
           firstName = first;
           lastName = last;
           socialSecurityNumber = ssn;
       } // end three-argument Employee constructor
       // set first name
       public void setFirstName( String first )
       {
          firstName = first; // should validate
       } // end method setFirstName
       // return first name
       public String getFirstName()
       {
          return firstName;
       } // end method getFirstName
       // set last name
       public void setLastName( String last )
       {
          lastName = last; // should validate
       } // end method setLastName
       // return last name
       public String getLastName()
       {
          return lastName;
       } // end method getLastName
       // set social security number
       public void setSocialSecurityNumber( String ssn )
       {
          socialSecurityNumber = ssn; // should validate
       } // end method setSocialSecurityNumber
       // return social security number
       public String getSocialSecurityNumber()
       {
          return socialSecurityNumber;
       } // end method getSocialSecurityNumber
       // return String representation of Employee object
       @Override
       public String toString()
       {
          return String.format( "%s %s\nsocial security number: %s",
          getFirstName(), getLastName(), getSocialSecurityNumber() );
       } // end method toString
// Note: We do not implement Payable method getPaymentAmount here so
// this class must be declared abstract to avoid a compilation error.
    } // end abstract class Employee
`

,最后一个公共类继承了这样的Employee:

 *public class SalariedEmployee extends Employee*

和提示错误就在这里:
“错误:SalariedEmployee不是抽象的,并且不会覆盖Payable公共类中的抽象方法getPaymentAmount()SalariedEmployee扩展Employee”

我不知道这是从哪里来的。这段代码来自一本书,应该完全没问题。我正在使用netbean 8.0和jdk 1.7

4 个答案:

答案 0 :(得分:1)

您的错误代码说明了一切。您必须从接口Playable实现每个抽象方法。 在这种情况下,您错过了getPaymentAmount()类中的方法SalariedEmployee

编辑:固定班级

答案 1 :(得分:1)

您必须实施getPaymentAmount()或抽象类(员工)或SalariedEmployee,因为他们实施Payable,此类包含函数getPaymentAmount()

答案 2 :(得分:0)

您必须按接口(getPaymentAmount())和抽象类定义的要求实现每个方法。

答案 3 :(得分:0)

好的我解决了这个问题只是因为我没有写出他所有的这个文件的代码。所以基本上是我们写行的时候

public class SalariedEmployee extends Employee

只有我们编写所有代码并添加方法

才会出现提示
public double getPaymentAmount()
{
return getWeeklySalary();
} // end method getPaymentAmount

到我们的程序然后错误将消失。我感谢支持。 像这样的文件SalariedEmployee.java的BTW代码

public class SalariedEmployee extends Employee
{
private double weeklySalary;
// four-argument constructor
public SalariedEmployee( String first, String last, String ssn,double salary )
{
super( first, last, ssn ); // pass to Employee constructor
setWeeklySalary( salary ); // validate and store salary
} // end four-argument SalariedEmployee constructor
// set salary
public void setWeeklySalary( double salary )
{
if ( salary >= 0.0 ) weeklySalary = salary;
else
throw new IllegalArgumentException(
"Weekly salary must be >= 0.0" );
} // end method setWeeklySalary
// return salary
public double getWeeklySalary()
{
return weeklySalary;
} // end method getWeeklySalary

// calculate earnings; implement interface Payable method that was
// abstract in superclass Employee
@Override
public double getPaymentAmount()
{
return getWeeklySalary();
} // end method getPaymentAmount
@Override
public String toString()
{
return String.format( "salaried employee: %s\n%s: $%,.2f",
super.toString(), "weekly salary", getWeeklySalary() );
} // end method toString
} // end class SalariedEmployee