我无法弄清楚为什么我的java继承不起作用

时间:2012-04-02 22:28:27

标签: java oop inheritance

对于家庭作业,我正在使用以下三个班级。

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

我无法让Faculty构造函数正确使用超类中的数据。

 import java.util.*;
 import javax.swing.*;
 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,  
                 department,ssn,salary);
           String booleanFlag = JOptionPane.showInputDialog(null, "Tenured (Y/N)?");
           if(booleanFlag.equals("Y"))
              tenured = true;
           else
              tenured = false;
      }
      public void setTenure(boolean tenured)
      {  this.tenured = tenured;   }
      public boolean getTenured()
      {  return tenured;  }
      public void display()
      {
          super.display();
          JOptionPane.showMessageDialog(null, "Tenured: " + tenured);
      }
 }

CollegeEmployee下降的Faculty类出现在下面。

 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 setFirstName(String firstName)
 {  this.firstName = firstName;  }
 public String getFirstName()
 {  return firstName;  }
     ... ETC ...  REMAINING GET/SET METHODS ELIMINATED FOR BREVITY.

我得到的错误指向参数之间的不匹配... Faculty调用八个参数,但CollegeEmployee只有五个。但是,我认为通过扩展CollegeEmployee扩展Person,我可以在调用此类时访问所有八个字段。正如已经指出的那样,情况并非如此。我只有Person的五个字段。所以我明显的下一个问题是我如何从ssn, department and salaryCollegeEmployee获得Faculty?那是我失踪的那件作品。我一直在研究Java教程并试验了几个小时,但仍然无法得到我需要做的修正。我是否需要通过People调用CollegeEmployee变量,然后在CollegeEmployee中实例化Faculty个变量?我对做什么感到非常困惑,迫切需要一些指导......

感谢加载,在我仔细阅读教程中的super()关键字部分后,我会回过头来看看。

2 个答案:

答案 0 :(得分:3)

构造函数中的super()调用调用父类构造函数。在这种情况下,父类CollegeEmployee的构造函数接受五个参数但是传递八个不正确。

请参阅Keyword super Tutorial,特别是子类构造函数部分。

答案 1 :(得分:2)

在Faculty.java中你有:     超级(名字,姓氏,的StreetAddress,邮政编码,phoneNumber的,部门,SSN,薪水);

这是物理上调用CollegeEmployee的构造函数,它只有五个参数。这是编译错误。

类可能包含未在构造函数中设置的变量。您可以拥有空构造函数并在另一个方法中设置变量。

由于存在继承结构,因此您只能通过super()设置父变量。