Java:ClassProblem。 Java新手

时间:2013-07-18 18:54:47

标签: java class

我正在处理的问题是我无法弄清楚如何在我的班级中显示某些值,它们只返回为零。我是初学者所以我对Java知之甚少,但我必须制作4个类或3个类型的员工和一个类来输出它们的值。但我无法获得佣金和工会雇员的价值观。这是代码:

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

1 个答案:

答案 0 :(得分:1)

我看到Employee字段设置的唯一位置是名为inputs()的方法

one的值已填充,因为您在inputs上调用了one方法,但对于您的UnionEmployee threeinputs之类的其他员工类型从未调用过,他们的领域永远不会被设定。

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

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

或者你可以复制它们

three.setName(one.getName());