2012-04-02 64 views
2

對於作業分配,我正在處理以下內容。這是一個指定的班級結構,我知道這不是一個遠射最好的設計。Java繼承和super()未按預期工作

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); 
    } 
} 
+1

其中一個構造函數的參數是'Department',你把它傳遞給超級「部門」。 – 2012-04-02 10:58:19

+1

糾正代碼之前,你應該縮進它,使所有不同的代碼看起來縮進! – MrJames 2012-04-02 11:06:19

+0

@ Eng.Fouad做出了這種情況下的糾正,還有一個我錯過的分號......但是現在還有另外一個相關的錯誤...:| – dwwilson66 2012-04-02 11:28:06

回答

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

是的。額外分號使構造函數體成爲一個完全合法的實例初始化塊。 – heikkim 2012-04-02 10:59:49

+0

@heikkim:但這個實例初始化塊中的超級調用不正確,對吧?調用super()後運行初始化塊() – thumbmunkeys 2012-04-02 11:02:56

+1

你是對的,事實並非如此。對不起,不明確。我只是指塊的位置,而不是塊的內容。 – heikkim 2012-04-02 11:12:13

4

在構造函數聲明行的末尾取出;

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

卸下「;」在'結束時,雙倍工資);'

public Faculty(String firstName, String lastName, String streetAddress, String zipCode, String phoneNumber,String ssn,String Department,double salary); <--- remove this 
     { 
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 
    } 
} 
+0

@ MrJames-我添加了我的CollegeEmployee類......你是說NEW變量(三個NOT是從' Person')應該在構造函數參數中進行投射?並且除了變量聲明之外或者代替變量聲明出現在構造函數的上面? – dwwilson66 2012-04-02 12:15:36

+0

我在說的是,在你的Faculty構造函數中,你調用超級(稱爲CollegeEmployee構造函數)有8個參數,但是在CollegeEmployee類中,你沒有帶8個參數的構造函數 – MrJames 2012-04-02 13:07:08