首页 > 解决方案 > 问题已执行,但未计算 netpay

问题描述

import java.util.Scanner;
class Employee{
    public String Staffname;
    public int StaffID;
    public int BasicSalary;
    public int Allowances;  
    public void Staff()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the Staff name=");
        this.Staffname=sc.nextLine();
        System.out.println("Enter the Staff ID=");
        this.StaffID=sc.nextInt();
        System.out.println("Enter the Basic Salary");
        this.BasicSalary=sc.nextInt();
        System.out.println("Enter the Allowances");
        this.Allowances=sc.nextInt();
        System.out.println("***Employee Details Registered Successfully***");
        System.out.println("");
    }
}
class Marketing extends Employee{
    public int sales_incentives;
    public int net_pay;
    public void Staff()
    {
        System.out.println(super.BasicSalary);
        System.out.println(super.Allowances);
        this.net_pay=super.BasicSalary+super.Allowances+this.sales_incentives;
        System.out.println("Net pay="+net_pay);
    }
    public static void main(String[] args){
        Marketing m=new Marketing();
        Employee e=new Employee();
        Scanner sc=new Scanner(System.in);
        e.Staff();
        System.out.println("Enter the incentives=");
        m.sales_incentives= sc.nextInt();
        m.Staff();
        
    }
}

这是一个用于覆盖方法的程序。该程序已执行但未计算 netpay。程序的输出如何从 super 调用基本工资和津贴来计算净工资。找不到错误。

标签: java

解决方案


如果要调用 super 方法,只需这样做:

public void Staff()
{
    super.Staff();

    System.out.println(super.BasicSalary);
    System.out.println(super.Allowances);
    this.net_pay=super.BasicSalary+super.Allowances+this.sales_incentives;
    System.out.println("Net pay="+net_pay);
}

推荐阅读