英文:
The problem is got executed but it doesn't calculate the 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();
}
}
这是一个用于方法重写的程序。程序已经执行,但未计算净薪水。如何从父类中调用基本工资和津贴以计算净薪水。找不到错误。
英文:
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();
}
}
This is a program for overriding method.The program got Executed but isnt calculating the netpay.The output of the program How to call the Basic salary and allowance from the super to calculate the Net pay. Can't able to find the mistake.
答案1
得分: 0
以下是翻译好的内容:
如果您想调用父类方法,只需按照以下方式进行:
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);
}
英文:
If you want to call the super method, just do it this way:
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);
}
答案2
得分: 0
m
对象未运行Employee::Staff()
,因此BasicSalary
和Allowances
未定义。
在Marketing::Staff()
方法顶部添加super.Staff()
。最好将激励查询也移到该方法中,这样您的抽象不会泄漏到main()
中。
英文:
The m
object did not run Employee::Staff()
, so BasicSalary
and Allowances
are undefined.
Add super.Staff()
to the top of Marketing::Staff()
method. Better yet, move the incentive query into that method, too, so your abstraction doesn't leak into main()
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论