gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 11:17:35 24 4
gpt4 key购买 nike

对于家庭作业,我正在处理以下内容。这是一个指定的类结构,我知道从长远来看这不是最好的设计。

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;
}
}

据我了解,Faculty 中的 super() 将允许访问 CollegeEmployee 以及 Person< 中的变量。使用上面的代码,当我只包含 Person 变量时,它可以正常编译。当我尝试使用ssn、department或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 类(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);
}
}

最佳答案

您在构造函数末尾意外创建了一个 ;:

      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部门

关于Java 继承和 super() 未按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9975109/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com