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

时间:2017-12-10 21:41:27

标签: java inheritance subclass superclass

我有一个超类,子类和测试器类。任何人都可以帮助解决我做错的事情吗?

以下是我在测试课程中所需要的内容 - 1.创建Manager和Employee的对象。

  1. 创建函数修订工资以修改Manager或Employee的构造函数中的工资集。 (使用多态的概念)

  2. 使用对象的显示功能打印与对象类相关的相关信息。

  3. 这是我的课程

    超类:

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

    子类:

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

    测试仪:

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

    **我的问题:

    我在测试课上遇到错误。当我创建一个管理器对象时,它说构造函数是未定义的。另外,对于我的私人空白" reviseSalary",它说我不能使用void

    任何人都可以告诉我我做错了什么,并在可能的情况下帮助创建我的修订功能

    由于

2 个答案:

答案 0 :(得分:2)

只需将你的修订评论从主要方法中删除。

  

发生此错误是因为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();
    }
}

这将是您的输出:

  
      
  • 员工人数:11111

         
        
    • 员工姓名:Elon Musk
    •   
    • 员工薪酬:42344.0
    •   
  •   
  • 员工人数:11111

         
        
    • 员工姓名:Elon Musk
    •   
    • 员工薪水:46578.4
    •   
  •   

答案 1 :(得分:0)

我发现您的代码存在两个主要问题:

1)您在reviseSalary()方法中声明了方法main()。 Java不允许您在另一个方法中声明方法。 您应该在main()方法之外声明它并添加关键字static,以便您可以调用此方法,而无需拥有Test类的实例。否则,如果您不将此方法声明为static,则需要一个Test类的实例,并且必须在此实例上调用reviseSalary(),如下所示:

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

2)您在Manager班级private int rewards中声明了此字段,但您没有声明任何setter方法为该字段分配任何值。