gpt4 book ai didi

Java:类问题。 Java新手

转载 作者:行者123 更新时间:2023-12-01 14:19:39 26 4
gpt4 key购买 nike

我正在处理的问题是,我无法弄清楚如何使类中的某些值出现,而它们仅返回为零。我是一名初学者,所以我对 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com