简体   繁体   English

[Java]为什么在我的测试人员课程中遇到这些错误?

[英][Java]Why am I getting these errors in my tester class?

I have a super class, sub class, and a tester class. 我有一个超级类,一个子类和一个测试器类。 Can anyone help with what I'm doing wrong? 谁能帮我解决我做错的事情?

Here is what I need in my tester class- 1. Create objects of Manager and Employee. 这是测试人员类中需要的-1.创建Manager和Employee的对象。

  1. Create a function revise salary to revise the salary set in the constructor of Manager or Employee. 创建一个功能修订工资来修订在经理或雇员的构造函数中设置的工资。 (Use concept of polymorphism) (使用多态的概念)

  2. Use the object's Display functions to print the relevant information pertaining to the object's class. 使用对象的“显示”功能可打印与对象类有关的相关信息。

Here are my classes 这是我的课

Superclass: 超类:

public class Employee {
    private int employeeNumber;
    private String employeeName;
    private double employeeSalary;

    public Employee(int employeeNumber, String employeeName, double employeeSalary) {
         this.employeeNumber = employeeNumber;
         this.employeeName = employeeName;
         this.employeeSalary = employeeSalary;
    }

         public double getEmployeeSalary() {
            return employeeSalary;
            }

         public void setEmployeeSalary(double employeeSalary) {
            this.employeeSalary = employeeSalary;
            }



         public void display(){
               System.out.println("Employee Number: "+ employeeNumber +"\n"
                     + "Employee Name: " + employeeName + "\n" 
                     + "Employee Salary: "  + employeeSalary);
            }

        }

Subclass: 子类:

public class Manager extends Employee {

    private int rewards;
    public Manager(int employeeNumber, String employeeName, double employeeSalary) {
        super(employeeNumber, employeeName, employeeSalary);
    }


    public void display() {
        super.display();
        System.out.println(rewards);
    }
}

Tester: 测试人员:

public class Test {
    public static void main(String [] args) {


         Manager manager = new Manager(11111, "Elon Musk", 42344);
          manager.display();     

         Employee employeeOne = new Employee(324, "Bob Den", 3522);
         Employee employeeTwo = new Employee(44, "Tim Pipe", 4234 );
         Employee employeeThree = new Employee(42, "Asif Blar", 4321);

         private void reviseSalary() {
         double employeeSalary = manager.getEmployeeSalary();
         manager.setEmployeeSalary(employeeSalary +(employeeSalary /10));
         manager.display();
         }

    }

}

**My issue: **我的问题:

I am getting errors on my test class. 我在测试课上遇到错误。 When I create a manager object, it says the constructor is undefined. 当我创建一个管理器对象时,它说构造函数是未定义的。 Also, for my private void "reviseSalary", it says I cannot use void 另外,对于我的私人空白“ reviseSalary”,它说我不能使用空白

Can anyone tell me what I'm doing wrong, and help in creating my reviseSalary function if possible 谁能告诉我我在做什么错,并在可能的情况下帮助创建我的reviseSalary函数

Thanks 谢谢

Just put your reviseSalary out of main method. 只需将reviseSalary置于主要方法之外。

This error happens because Java does not support nested function. 发生此错误的原因是Java不支持嵌套函数。

    public class Test {
    public static void main(String [] args) {


        Manager manager = new Manager(11111, "Elon Musk", 42344);
        manager.display();

        Employee employeeOne = new Employee(324, "Bob Den", 3522);
        Employee employeeTwo = new Employee(44, "Tim Pipe", 4234 );
        Employee employeeThree = new Employee(42, "Asif Blar", 4321);

        reviseSalary(manager);
    }
    private static void reviseSalary(Manager manager) {
        double employeeSalary = manager.getEmployeeSalary();
        manager.setEmployeeSalary(employeeSalary +(employeeSalary /10));
        manager.display();
    }
}

This will be your output: 这将是您的输出:

  • Employee Number: 11111 员工编号:11111

    • Employee Name: Elon Musk 员工姓名:Elon Musk
    • Employee Salary: 42344.0 员工薪资:42344.0
  • Employee Number: 11111 员工编号:11111

    • Employee Name: Elon Musk 员工姓名:Elon Musk
    • Employee Salary: 46578.4 员工薪资:46578.4

I see two main problems with your code: 我发现您的代码存在两个主要问题:

1) You declare method reviseSalary() inside main() method. 1)您在main()方法中声明方法reviseSalary() Java doesn't allow you to declare method inside of the other method. Java不允许您在其他方法内部声明方法。 You should rather declare it outside of the main() method and add a keyword static to let you call this method without the need of having an instance of your Test class. 您应该在main()方法之外声明它,并添加关键字static以使您无需拥有Test类的实例即可调用此方法。 Otherwise, if you don't declare this method as static , you would need an instance of Test class and have to call reviseSalary() on this instance like this: 否则,如果您不将此方法声明为static ,则将需要Test类的实例,并且必须在此实例上调用reviseSalary() ,如下所示:

Test t = new Test() ;
t.reviseSalary();

2) You declared this field in the Manager class: private int rewards , but you don't declare any setter method to assign any value to that field. 2)您在Manager类中声明了此字段: private int rewards ,但是您没有声明任何setter方法来为该字段分配任何值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM