Java继承和super()没有按预期工作

时间:2012-04-02 10:55:02

标签: java oop inheritance

对于家庭作业,我正在使用以下内容。这是一个指定的类结构,我知道这不是最好的设计。

Class           | Extends         | Variables
--------------------------------------------------------
Person          | None            | firstName, lastName, streetAddress, zipCode, phone
CollegeEmployee | Person          | ssn, salary,deptName
Faculty         | CollegeEmployee | tenure(boolean)
Student         | person          | GPA,major

所以在教师班......

 public class Faculty extends CollegeEmployee
 {
      protected String booleanFlag;
      protected boolean tenured;
      public Faculty(String firstName, String lastName, String streetAddress,
                     String zipCode, String phoneNumber,String ssn,
                     String department,double salary)
      { 
           super(firstName,lastName,streetAddress,zipCode,phoneNumber,
                 ssn,department,salary);
           String booleanFlag = JOptionPane.showInputDialog
                                (null, "Tenured (Y/N)?");
           if(booleanFlag.equals("Y"))
           tenured = true;
           else
           tenured = false;
       }
 }

据我了解,super()中的Faculty将允许访问CollegeEmployee以及Person中的变量。使用上面的代码,当我只包含Person变量时,它编译得很好。一旦我尝试使用ssn, department, or salary,我就会收到以下编译错误。

 Faculty.java:15: error: constructor CollegeEmployee in class CollegeEmployee can not be applied to the given types:
            super(firstName,lastName,streetAddress,zipCode,phoneNumber,ssn,department,salary);
                                                                                            ^
Required: String,String,String,String,String
   Found: String,String,String,String,String,String,String,String
  reason: actual and formal argument lists differ in length

我完全被这个错误搞糊涂了......这是真实的还是正式的? Person有五个参数,CollegeEmployee有3个,所以我的猜测是参数传递方式有点时髦......但我不太确定从哪里开始修复它。我错过了什么?

这是我的CollegeEmployee类

 import java.util.*;
 import javax.swing.*;
 //
 public class CollegeEmployee extends Person
 {
     protected String ssn;
     protected String sal;
     protected double annSalary;
     protected String department;
 //
     public CollegeEmployee(String firstName, String lastName, 
                            String streetAddress, String zipCode, 
                            String phoneNumber)
     {
          super(firstName,lastName,streetAddress,zipCode,phoneNumber);
          ssn = JOptionPane.showInputDialog(null, "Enter SSN ");
          department = JOptionPane.showInputDialog
                       (null, "Enter department: ");
          sal = JOptionPane.showInputDialog(null, "Enter salary: ");
         annSalary = Double.parseDouble(sal);
     }
 //
     public void setSSN(String ssn)
     {         this.ssn = ssn;         }
     public void setAnnSalary(double annSalary)
     {        this.annSalary = annSalary;     }
     public void setDepartment(String department)
     {        this.department = department;    }
 //
     public String getSSN()
     {        return ssn;                }
     public double getAnnSalary()
     {        return annSalary;          }
     public String getDepartment()
     {        return department;         }
 //
     public void display()
     {
         super.display();
         JOptionPane.showMessageDialog
         (null, "\nSSN: " + getSSN() + "\nAnnual Salary: " 
          + getAnnSalary() + "\nDepartment: " + getDepartment(), 
          "Information", JOptionPane.QUESTION_MESSAGE);
     }
 }

4 个答案:

答案 0 :(得分:7)

您在构造函数的末尾发生了意外的;

      public Faculty(String firstName, String lastName, String streetAddress,   String zipCode, String phoneNumber,String ssn,
           String Department,double salary); // <--- this has to go

正如评论中指出的那样,您的构造函数中包含String Department,但是您传递了department(大小写不正确),请将构造函数参数重命名为String department

答案 1 :(得分:4)

删除构造函数声明行末尾的;

public Faculty(String firstName, String lastName, String streetAddress, String zipCode, String phoneNumber,String ssn,String Department,double salary);
//----------------------------------------------------------------------------------------------------------------------------------------------------^

答案 2 :(得分:4)

删除';'在'结束,双倍薪水';'

public Faculty(String firstName, String lastName, String streetAddress, String zipCode, String phoneNumber,String ssn,String Department,double salary); <--- remove this
      {

答案 3 :(得分:1)

你的超级CollegeEmployee需要有一个带有这八个参数的构造函数,参数类型也应匹配

public class CollegeEmployee
{
    public CollegeEmployee(String firstName, String lastName, String streetAddress, String zipCode,
                        String phoneNumber, String ssn, String department, double salary)
    { 
        // do stuff
    }
}