Java继承和super()不能按预期工作 [英] Java inheritance and super() isn't working as expected
问题描述
对于一项家庭作业,我正在从事以下工作.从长远来看,这是一个分配的类结构,我知道这不是最佳的设计.
For a homework assignment, I'm working with the following. It's an assigned class structure, I know it's not the best design by a long shot.
Class | Extends | Variables
--------------------------------------------------------
Person | None | firstName, lastName, streetAddress, zipCode, phone
CollegeEmployee | Person | ssn, salary,deptName
Faculty | CollegeEmployee | tenure(boolean)
Student | person | GPA,major
所以在教师班上...
So in the Faculty class...
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;
}
}
据我了解,Faculty
中的super()
将允许访问CollegeEmployee
和Person
中的变量.使用上面的代码,当我仅包含Person
变量时,它可以正常编译.尝试使用ssn, department, or salary
时,立即出现以下编译错误.
It was my understanding that super()
in Faculty
would allow access to the variables in CollegeEmployee
as well as Person
. With the code above, it compiles fine when I ONLY include the Person
variables. As soon as I try to use ssn, department, or salary
I get the following compile errors.
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个参数,所以我猜想参数传递的方式有些奇怪...但是我不太确定从哪里开始修复它.我想念什么?
I'm completely confused by this error...which is the actual and formal? Person
has five arguments, CollegeEmployee
has 3, so my guess is that something's funky with how the parameters are being passed...but I'm not quite sure where to begin fixing it. What am I missing?
这是我的CollegeEmployee班
Here's my CollegeEmployee class
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);
}
}
推荐答案
您在构造函数的结尾意外创建了;
:
You made an accidental ;
at the end of your constructor:
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
And as pointed out in a comment you have String Department
in your constructor, but you pass department
(incorrect capitalization), rename your constructor argument to String department
这篇关于Java继承和super()不能按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!