Java:类问题。 Java新手

标签 java class

我正在处理的问题是,我无法弄清楚如何使类中的某些值出现,而它们仅返回为零。我是一名初学者,所以我对 Java 不太了解,但我必须创建 4 个类或 3 种类型的员工和一个类来输出他们的值。但我无法让委员会和工会雇员表现出值(value)观。这是代码:

import java.util.Scanner;

// Base or Superclass
class Employee
{
    String name;
    String department;
    double pay;
    double hours;
    double money;
    double mission;
    double rate;
    double withheld;
    double moneyc;
    double moneyu;
    double sales;

    public void inputs()
    {
        Scanner in = new Scanner(System.in);

        System.out.println("Enter your name: ");
        name = in.nextLine();

        System.out.println("Enter your department: ");
        department = in.nextLine();

        System.out.println("Enter your pay per hour: ");
        pay = in.nextDouble();

        System.out.println("Enter how many hours you worked: ");
        hours = in.nextDouble();

        System.out.println("Enter your rate of commission(0.00): ");
        rate = in.nextDouble();

        System.out.println("Enter your withheld amount: ");
        withheld = in.nextDouble();

        System.out.println("Enter your sales amount: ");
        sales = in.nextDouble();

        money = pay * hours;
    }

    // Accessor Methods
    public String getName()
    {
        return this.name;
    }

    public String getDepartment()
    {
        return this.department;
    }

    public Double getPay()
    {
        return this.pay;
    }

    public Double getHours()
    {
        return this.hours;
    }

    public Double getMoney()
    {
        return this.money;
    }

    public Double getMission()
    {
        return this.mission;
    }

    public Double getRate()
    {
        return this.rate;
    }

    public Double getWithheld()
    {
        return this.withheld;
    }

    public Double getMoneyc()
    {
        return this.moneyc;
    }

    public Double getMoneyu()
    {
        return this.moneyu;
    }

    public Double getSales()
    {
        return this.sales;
    }

    // Mutator Methods
    public void setName(String n)
    {
        name = n;
    }

    public void setDepartment(String d)
    {
        department = d;
    }

    public void setPay(double p)
    {
        pay = p;
    }

    public void setHours(double h)
    {
        hours = h;
    }

    public void setMoney(double m)
    {
        money = m;
    }

    public void setMission(double mi)
    {
        mission = mi;
    }

    public void setRate(double r)
    {
        rate = r;
    }

    public void setWithheld(double w)
    {
        withheld = w;
    }

    public void setMoneyc(double mc)
    {
        moneyc = mc;
    }

    public void setMoneyu(double mu)
    {
        moneyu = mu;
    }

    public void setSales(double s)
    {
        sales = s;
    }
}

class Last extends Employee
{
    public void dinero()
    {
        Employee one = new Employee();

        one.inputs();

        // Union Employee
        UnionEmployee three = new UnionEmployee();
        three.Syndicate();

        // Commission Employee
        Commissioned two = new Commissioned();
        two.sales();

        System.out.println("\n"+ "Name: "+ one.getName());
        System.out.println( "Department: "+ one.getDepartment());
        System.out.println( "Hours: "+ one.getHours());
        System.out.println( "Pay Rate: "+ one.getPay());
        System.out.println("Your money is: "+ one.getMoney());

        // Commissioned Employee
        System.out.println( "\n"+ "Commissioned Employee");
        System.out.println("Your money is: "+ one.getMoneyc());
        System.out.println( "Your commission is: "+ one.getMission());
        System.out.println("Your rate: "+ one.getRate());

        // Union employee
        System.out.println("\n"+"Union Employee");
        System.out.println("Your money is: "+ one.getMoneyu());
        System.out.println( "Your withheld is: "+ one.getWithheld());
    }
}

// Derived or Subclass
class Commissioned extends Employee
{
    public void sales()
    {
        moneyc = hours * pay;
        // Commission
        mission = sales * rate;
    }
}

// Derived or Subclass
class UnionEmployee extends Employee
{
    public void Syndicate()
    {
        if (hours <= 40) {
            moneyu = (hours * pay) - withheld;
        } else  {
            moneyu = (pay * hours * ((hours - 40) * 1.5)) - withheld;
        }
    }
}

public class WeiTry extends Last
{
    public static void main(String[] args)
    {
        // Output
        Last show = new Last();
        show.dinero();
    }
}

最佳答案

我看到您的 Employee 字段被设置的唯一地方是在名为 inputs() 的方法内

one 的值会被填充,因为您对 one 调用 inputs 方法,但对于其他员工类型(例如 UnionEmployee)三个输入从未被调用,并且它们的字段从未被设置。

如果您希望设置其他员工的值,那么您似乎必须调用他们的输入方法。

UnionEmployee three = new UnionEmployee();
three.inputs();
three.Syndicate();

或者你可以直接复制它们

three.setName(one.getName());

关于Java:类问题。 Java新手,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17731689/

相关文章:

python - 使用来自不同文件 pygame 的 Sprite 对象类

c++ - 在这种情况下我应该使用引用成员吗?

java - 类测试期间出现 NullPointerException

java - 如何在 ActionListener 的 actionPerformed 中传递 for 循环索引 [Java]

java - log4j - 写入/记录到多个日志文件取决于应用程序

使用 maven exec-maven-plugin “target” 文件夹生成的 Java 类未编译

java - 当目标路径是目录时,Files.newInputStream() 的无关紧要的行为?

java - JTable 没有从数据库附加数据

python - 为什么从类 'object' 继承我们创建的任何类是一个好习惯?

python - 为什么我第二次运行类方法时会收到 TypeError?