Java类“无法解析为类型”

时间:2011-02-26 04:38:30

标签: java constructor

这是我得到的错误:

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 个答案:

答案 0 :(得分:4)

问题是TeamLeader似乎是一个内部类(很难说,你的缩进很糟糕),并且因为它不是静态的,所以你不能自己实例化它。

您需要将TeamLeader作为自己的类,或者将其设为static类并将其实例化为Employee.TeamLeader(或者无论父类是什么,您的缩进都没有帮助这里)。

答案 1 :(得分:3)

您的异常消息说明了

  

线程“main”中的异常java.lang.Error:未解决的编译问题:TeamLeader无法解析为类型

这意味着您已尝试运行使用某些未正确编译的类的程序。

返回并修复编译错误。

目前尚不清楚编译错误的修复方法应该是什么,但错误是说它无法找到名为TeamLeader的类。也许它不存在。也许它是在一个不同的包中。也许你没有编译它。也许别的什么。


查看代码,我认为问题在于您定义了两个不同的类,名为TeamLeadDemo,一个作为嵌套类Employee,第二个作为顶级课程。此外,其中第二个尝试使用TeamLeader ...但您实际声明的TeamLeader类在Employee中嵌套了2个级别;即它的真实姓名是Employee.ShiftSupervisor.TeamLeader

这有点荒谬。

从表面上看,你在同一个文件(“Employee.java”)中定义了一大堆类,但没有太多理解将一个类放在另一个类中的含义。最有可能的是,每个类都应该在自己的文件中声明。您最有可能只删除嵌套的TeamLeadDemo类。

答案 2 :(得分:1)

如果此问题与maven项目有关,请右键单击 Maven>更新项目应解决问题

答案 3 :(得分:0)

如果通过从某个位置移动内部类来创建内部类,也会出现该错误,并且通过重组,您将该内部类的导入行放在其外部类文件中。之后,内部类cannot be resolved to a type随处可见。自动插入导入无济于事。

您尚未发布外部类的导入部分,我们无法检查该变体。检查它,如果有过多的导入,请将其删除。

如果您在Eclipse中工作,那么很可能您需要在此之后清理项目。