如何为从子类到主类的方法调用添加值?

问题描述 投票:0回答:1

似乎出现了字符串值,但我无法在全职和兼职计算中添加值。

这是我继承的主类

import java.util.Scanner;
public class RunEmployee1 {
    private static String name;
    private static double rate;
    private static int hour;
    private static double wage1;
    private static double salary;
    public String getName(){
        return name;
    }
    public double getMonthlySalary(){
        return salary;
    }
    public double getWage1(){
        return wage1;
    }
    public static void main(String[]args){
        char position;
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter Employee name: ");
        name = sc.nextLine();
        System.out.println("Type F for Full time or P for Part time: ");
        System.out.println("Use Capital Letter only!");
        position = sc.next().charAt(0);
        switch(position){
            case 'F':
                System.out.println("You are a Full time Employee!");
                System.out.println("Enter your Monthly Wage: ");
                salary = sc.nextDouble();
                break;
            case 'P':
                System.out.println("You are a Part time Employee!");
                System.out.println("Enter the number of hours you worked in: ");
                hour = sc.nextInt();
                System.out.println("Enter the hourly rate you are working in: ");
                rate = sc.nextDouble();
                break;
            default :
                System.out.println("You are not an Employee!");
        }
        Employee1 em1 = new Employee1();
        FullTimeEmployee1 em2 = new FullTimeEmployee1();
        PartTimeEmployee1 em3 = new PartTimeEmployee1();
        em1.getName();
        em1.writeOutput1();
        em2.getMonthlySalary();
        em2.writeOutput2(salary);
        em3.getWage1();
        em3.writeOutput3(wage1);
    }
}

这是儿童班

import java.util.Scanner;
public class FullTimeEmployee1 extends RunEmployee1{
    private static double salary;
    private static double newMonthlySalary;
    Scanner sc = new Scanner(System.in);
    public void setMonthlySalary(double newMonthlySalary){
        this.salary = newMonthlySalary;
        newMonthlySalary = sc.nextDouble();
    }
    public double getMonthlySalary(double newMonthlySalary){
        salary = newMonthlySalary;
        return salary = newMonthlySalary;
    }
    public void writeOutput2(double getMonthlySalary){
        System.out.println("Employee's Full time Monthly Salary is: " +getMonthlySalary());
    }
    public static void main(String[]args){
        Scanner sc = new Scanner(System.in);
        FullTimeEmployee1 em2 = new FullTimeEmployee1();
        System.out.println("You are a Full time Employee!");
        System.out.println("Enter your Monthly Wage: ");
        salary = sc.nextDouble();
        em2.getMonthlySalary();
        em2.writeOutput2(salary);
    }
}
 and 

import java.util.Scanner;
public class PartTimeEmployee1 extends RunEmployee1 {
    private static double rate;
    private static int hour;
    private static double wage1;
    private static double newMonthlyWage1;
    Scanner sc = new Scanner(System.in);
    public void setWage1(int hourWorked, double RatePerHour, double newMonthlyWage) {
        RatePerHour = rate;
        hourWorked = hour;
        double wage1 = RatePerHour * hourWorked;
        this.wage1 = newMonthlyWage;
        newMonthlyWage = sc.nextDouble();
    }
    public double getWage1(double newMonthlyWage) {
        wage1 = newMonthlyWage;
        return wage1 = newMonthlyWage;
    }
    public void writeOutput3(double getWage1) {
        System.out.println("Employee's Part time Monthly Wage is: " + +getWage1());
    }
    public static void main(String[]args){
        Scanner sc = new Scanner(System.in);
        PartTimeEmployee1 em3 = new PartTimeEmployee1();
        System.out.println("You are a Part time Employee!");
        System.out.println("Enter the number of hours you worked in: ");
        hour = sc.nextInt();
        System.out.println("Enter the hourly rate you are working in: ");
        rate = sc.nextDouble();
        wage1 = rate * hour;
        em3.getWage1();
        em3.writeOutput3(wage1);
    }
}

我应该显示全职和兼职子类的薪资值结果。

输入员工姓名:姓名 输入 F(全职)或 P(兼职): 仅使用大写字母! P 您是兼职员工!输入 您工作的小时数:5 输入您的时薪 工作地点: 100 员工姓名为: 姓名 员工全职 每月 工资是:0.0 员工兼职月工资是:0.0

expected result with value for both the salary

java inheritance subclass extends method-call
1个回答
0
投票

您的代码中似乎有几个问题需要解决,才能正确计算和显示全职和兼职员工的工资。我将一一解决这些问题并对您的课程提出修改建议。

一般问题和建议

1)静态变量: 您在应该使用实例变量的地方使用静态变量。静态变量在类的所有实例之间共享,这不适合特定于单个员工的姓名、费率、薪水等属性。

2)继承模型: 您的继承模型似乎被误用了。扩展 RunEmployee1 的 FullTimeEmployee1 和 PartTimeEmployee1 类(似乎旨在作为运行程序的主类)不是继承的典型用例。通常,您将有一个基类 Employee,然后 FullTimeEmployee 和 PartTimeEmployee 将扩展 Employee。

3)方法定义和用法: 您的 setMonthlySalary、getMonthlySalary、setWage1 和 getWage1 方法未正确实现。例如,在 FullTimeEmployee1 中,您在 setter 方法中要求输入,这不是一个好的做法。同样,getMonthlySalary 方法不应该需要参数来返回薪水。

让我们重构您的代码来解决这些问题。首先,我们将定义一个 Employee 基类,然后创建扩展该基类的 FullTimeEmployee 和 PartTimeEmployee 类。我们还将修复与静态变量和方法使用相关的问题。

基础员工班

public class Employee {
protected String name;
protected double rate;
protected int hour;

public Employee(String name) {
    this.name = name;
}

public String getName() {
    return name;
}

// Additional common methods for Employee
}

全职员工班

public class FullTimeEmployee extends Employee {
private double salary;

public FullTimeEmployee(String name, double salary) {
    super(name);
    this.salary = salary;
}

public double getMonthlySalary() {
    return salary;
}

public void writeOutput() {
    System.out.println("Employee's Full time Monthly Salary is: " + salary);
}
}

兼职员工班

public class PartTimeEmployee extends Employee {
private double wage;

public PartTimeEmployee(String name, int hour, double rate) {
    super(name);
    this.hour = hour;
    this.rate = rate;
    this.wage = hour * rate;
}

public double getWage() {
    return wage;
}

public void writeOutput() {
    System.out.println("Employee's Part time Monthly Wage is: " + wage);
}
}

主班 现在,让我们调整您的主类以使用这些重构的类:

import java.util.Scanner;

public class RunEmployee {
public static void main(String[]args) {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter Employee name: ");
    String name = sc.nextLine();
    System.out.println("Type F for Full time or P for Part time: ");
    System.out.println("Use Capital Letter only!");
    char position = sc.next().charAt(0);
    Employee employee = null;

    switch(position) {
        case 'F':
            System.out.println("You are a Full time Employee!");
            System.out.println("Enter your Monthly Wage: ");
            double salary = sc.nextDouble();
            employee = new FullTimeEmployee(name, salary);
            break;
        case 'P':
            System.out.println("You are a Part time Employee!");
            System.out.println("Enter the number of hours you worked in: ");
            int hour = sc.nextInt();
            System.out.println("Enter the hourly rate you are working in: ");
            double rate = sc.nextDouble();
            employee = new PartTimeEmployee(name, hour, rate);
            break;
        default :
            System.out.println("You are not an Employee!");
            System.exit(0);
    }

    if (employee instanceof FullTimeEmployee) {
        ((FullTimeEmployee) employee).writeOutput();
    } else if (employee instanceof PartTimeEmployee) {
        ((PartTimeEmployee) employee).writeOutput();
    }
}
}

这些更改应该可以帮助您正确计算和显示全职和兼职员工的工资,遵守更好的面向对象原则和 Java 最佳实践。

© www.soinside.com 2019 - 2024. All rights reserved.