對於作業分配,我正在處理以下內容。這是一個指定的班級結構,我知道這不是一個遠射最好的設計。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);
}
}
其中一個構造函數的參數是'Department',你把它傳遞給超級「部門」。 – 2012-04-02 10:58:19
糾正代碼之前,你應該縮進它,使所有不同的代碼看起來縮進! – MrJames 2012-04-02 11:06:19
@ Eng.Fouad做出了這種情況下的糾正,還有一個我錯過的分號......但是現在還有另外一個相關的錯誤...:| – dwwilson66 2012-04-02 11:28:06