gpt4 book ai didi

java - 继承Java super.print()不返回值

转载 作者:行者123 更新时间:2023-12-01 09:25:18 25 4
gpt4 key购买 nike

我的代码不会打印出佣金的付费、当前时间和总销售额,我认为这可能与继承有关,但我不确定。我在另一个类(class)选角,但我认为问题就在这里,因为我的其他类(class)的选角没有问题。除了佣金的 3 个值之外,程序中返回所有值。工资和当前工作时间按小时更新,佣金是从中继承的,但它在佣金类中不起作用。

//StaffMember Class
public abstract class StaffMember {
protected String name, address, number;

public StaffMember(String name, String address, String number) {
this.name = name;
this.address = address;
this.number = number;
}

public String toString(){
return "Name: " +name + "\nAddress: " +address + "\nPhone: " +number;
}

public abstract double pay();
}

//Employee Class
public class Employee extends StaffMember{
private String rsiNumber;
protected double payRate;

public Employee(String rsiNumber, double payRate, String name, String address, String number) {
super(name, address, number);
this.rsiNumber = rsiNumber;
this.payRate = payRate;
}

public String toString(){
return super.toString() + "\nRSI Number: " +rsiNumber + String.format("\nPaid: €%,.2f" ,pay());
}

public double pay(){
return payRate;
}
}


//Hourly Class
public class Hourly extends Employee{
private int hours;

public Hourly(String rsiNumber, double payRate, String name, String address, String number) {
super(rsiNumber, payRate, name, address, number);
this.hours = 0;
}

public void addHours(int hours){
this.hours=hours;
}

public double pay(){
int otHours=0;
if(hours>40){
otHours=hours-40;
}
double pay=(hours-otHours)*payRate+(otHours*payRate*1.5);
return pay;
}

public String toString(){
return super.toString() + "\nCurrent Hours: " +hours;
}
}


//Commission Class
public class Commission extends Hourly{
private double totalSales, commissionRate;

public Commission(double commissionrate, String rsiNumber, double payRate, String name, String address, String number) {
super(rsiNumber, payRate, name, address, number);
this.commissionRate = commissionrate;
this.totalSales=0;
}

public void addSales(int sales){
this.totalSales=sales;
}

public double pay(){
return super.pay()+(totalSales*commissionRate);
}

public String toString(){
return super.toString() + "\nTotal Sales: " +totalSales;
}
}


//Casting Method From Another Class
public void processStaff(){
for(int i=0; i<sm.length; i++){
if (sm[i] instanceof Hourly) {
Hourly h = (Hourly) sm[i];
if(h.name.equals("Joanne")){
h.addHours(50);
}
}else if (sm[i] instanceof Executive) {
Executive e = (Executive) sm[i];
if(e.name.equals("John")){
e.awardBonus(500);
}
}else if (sm[i] instanceof Commission) {
Commission c = (Commission) sm[i];
if(c.name.equals("Molly")){
c.addHours(55);
c.addSales(400);
}else if(c.name.equals("Joe")){
c.addHours(45);
c.addSales(950);
}
}
}
}

这是当我应该返回值而不是 0 时得到的输出。

编辑:似乎已经找到了解决方案,只需将佣金放入每小时中,因为佣金是每小时的一种类型。

public void processStaff() {
for (int i = 0; i < sm.length; i++) {
if (sm[i] instanceof Hourly) {
Hourly h = (Hourly) sm[i];
if (h.name.equals("Joanne")) {
h.addHours(50);
} else if (sm[i] instanceof Commission) {
Commission c = (Commission) sm[i];
if (c.name.equals("Molly")) {
c.addHours(55);
c.addSales(400);
} else if (c.name.equals("Joe")) {
c.addHours(45);
c.addSales(950);
}
}
} else if (sm[i] instanceof Executive) {
Executive e = (Executive) sm[i];
if (e.name.equals("John")) {
e.awardBonus(500);
}
}
}
}

最佳答案

       if (sm[i] instanceof Hourly) {

/* .... */
}else if (sm[i] instanceof Commission) {
Commission c = (Commission) sm[i];
if(c.name.equals("Molly")){
c.addHours(55);
c.addSales(400);
}else if(c.name.equals("Joe")){
c.addHours(45);
c.addSales(950);
}
}

第一个条件对于Commission 成立,因此您永远不会对其调用addHours 或addSales。因此没有任何可显示的内容。

您确实应该使用调试器查看类的内容。

关于java - 继承Java super.print()不返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39882882/

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