这是我得到的错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
TeamLeader cannot be resolved to a type
at TeamLeadDemo.main(TeamLeadDemo.java:26)
这是我的代码:
import java.util.Scanner;
public class Employee {
public String empName, empNumber, hireDate;
public class TeamLeadDemo {}
public Employee(String empName, String empNumber, String hireDate) {
this.setEmpName(empName);
this.setEmpNumber(empNumber);
this.setHireDate(hireDate);
}
public void setEmpName(String empName) {
this.empName = empName;
}
public void setEmpNumber(String empNumber) {
this.empNumber = empNumber;
}
public void setHireDate(String hireDate) {
this.hireDate = hireDate;
}
public String getEmpName() {
return empName;
}
public String getEmpNumber() {
return empNumber;
}
public String getHireDate() {
return hireDate;
}
public class ShiftSupervisor extends Employee {
public double annualSalary, annualProduction;
//constructor
public ShiftSupervisor(String empName, String empNumber,
String hireDate, double annualSalary,
double annualProduction) {
super(empName,empNumber, hireDate);
this.setAnnualSalary(annualSalary);
this.setAnnualProduction(annualProduction);
}
public double getAnnualSalary() {
return annualSalary;
}
public double getAnnualProduction() {
return annualProduction;
}
public void setAnnualSalary(double annualSalary) {
this.annualSalary = annualSalary;
}
public void setAnnualProduction(double annualProduction) {
this.annualProduction = annualProduction;
}
public String toString() {
return "Name: "+ getEmpName() + "\nEmpID: "+ getEmpNumber()
+ "\nHire Date: "+ getHireDate() + "\nAnnual Salary: "
+ annualSalary + "\nProduction: "+ annualProduction;
}
public class employeeStart {
public void main(String[] args) {
String name, id, date;
double sal, prod;
//create scanner object
Scanner keyboard = new Scanner(System.in);
//inputting data
System.out.println("Enter Name: ");
name = keyboard.nextLine();
System.out.println("Enter id: ");
id = keyboard.nextLine();
System.out.println("Enter Hire Date: ");
date = keyboard.nextLine();
System.out.println("Enter Annual: ");
sal = keyboard.nextDouble();
System.out.println("Enter production: ");
prod = keyboard.nextDouble();
//instantiating object
ShiftSupervisor pw = new ShiftSupervisor(name, id, date, sal, prod);
//outputting data
System.out.println("Employee Details: \n" + pw);
}
}
public class TeamLeader {
public double monthlyBonus;
public int minTraining, trainingPresent;
public TeamLeader(double monthlyBonus, int minTraining, int trainingPresent) {
this.setMonthlyBonus(monthlyBonus);
this.setMinTraining(minTraining);
this.addtrainingPresent(trainingPresent);
}
public void setMonthlyBonus(double monthlyBonus) {
this.monthlyBonus = monthlyBonus;
}
public void setMinTraining(int minTraining) {
this.minTraining = minTraining;
}
public void setTrainingPresent(int t) {
trainingPresent = t;
}
public void addtrainingPresent(int hours) {
trainingPresent += hours;
}
public double getMonthlyBonus() {
return monthlyBonus;
}
public int getMinTraining() {
return minTraining;
}
public int getTrainingPresent() {
return trainingPresent;
}
public String toString() {
return "Bonus: "+ getMonthlyBonus() + "\nMinimum Training: "
+ getMinTraining() + "\nAttendence: "+ getTrainingPresent();
}
}
}
}
此外,我在一个单独的类中声明了这一点:
import java.util.Scanner;
public class TeamLeadDemo extends Employee {
public TeamLeadDemo(String empName, String empNumber, String hireDate) {
super(empName, empNumber, hireDate);
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
double sal;
int min, atten;
//create scanner object
Scanner keyboard = new Scanner(System.in);
//inputting data
System.out.println("Enter minimum training: ");
min = keyboard.nextInt();
System.out.println("Enter id: ");
atten = keyboard.nextInt();
System.out.println("Enter Bonus: ");
sal = keyboard.nextDouble();
//instantiating object
ShiftSupervisor pw = new TeamLeader(sal, min, atten);
//outputting data
System.out.println("Employee Details:\n" + pw);
}
}
导致此错误的原因是什么?我应该如何解决它?
编辑:缩进、空格、命名约定和可读性问题已经得到了一定程度的解决。
4条答案
按热度按时间z31licg01#
如果此问题与maven项目有关,则右键单击Maven〉Update Project应解决此问题
h43kikqp2#
问题是
TeamLeader
看起来像是一个内部类(很难说,因为您的缩进不好),而且由于它不是静态的,您不能示例化它本身。您要么需要使
TeamLeader
成为它自己的类,要么使它成为static
类并将其示例化为Employee.TeamLeader
(或者不管父类是什么,这里的缩进实际上没有帮助)。6ovsh4lw3#
您的异常消息如下所示
线程“main”java.lang中出现异常。错误:未解决的编译问题:TeamLeader无法解析为类型
这意味着您试图运行的程序使用了某些未正确编译的类。
返回并修复编译错误。
目前还不清楚应该如何修复编译错误,但该错误表示找不到名为
TeamLeader
的类。可能该类不存在,可能在不同的包中,可能您尚未编译它,也可能是其他原因。查看代码,我认为问题在于您定义了 * 两个不同的类 *,称为
TeamLeadDemo
,一个作为Employee
的嵌套类,另一个作为顶层类,而且第二个试图使用TeamLeader
......但是您实际声明的TeamLeader
类在Employee
中嵌套了两层;即它的真实的名称是X1 M6 N1 X。这一切都有点荒谬。
看起来,你在同一个文件(“www.example.com“)中定义了一大堆类Employee.java,却没有太多地理解将一个类放在另一个类中意味着什么。最有可能的是,这些类中的每一个都应该在自己的文件中声明。最有可能的是,你应该删除嵌套的
TeamLeadDemo
类。knsnq2tg4#
这个错误也会出现,如果你创建内部类的时候把它从某个地方移过来,然后重组,把这个内部类的import行放到它的外部类文件中,然后把内部类
cannot be resolved to a type
放到任何地方,自动插入import也没用。您尚未发布外部类的导入节,我们无法检查该变量。请检查该变量,如果存在过多的导入,请将其移除。
如果您在Eclipse中工作,则很可能需要在此之后清理项目。