gpt4 book ai didi

使用继承时会出现 Java 操作数错误,但在单个类上执行相同操作时不会出现错误

转载 作者:行者123 更新时间:2023-12-02 06:13:21 25 4
gpt4 key购买 nike

首先,是的,这是家庭作业(但我正在自己做作业)。我一直在努力工作,但我不明白我做错了什么。

该作业要求销售人员的基本工资和佣金分为两类。

我确信您之前已经看过此内容,但我找不到涉及此问题的其他帖子。

我的基本工资等级。

package salescommission;

public class SalesBaseSalary {

public SalesBaseSalary() {

int baseSalary = 75000;

}
}

以及我的销售佣金类别

package salescommission;

//Adding Scanner Utility
import java.util.Scanner;


public class SalesCommission extends SalesBaseSalary {
public static void main(String[] args) {
//Scanner input
Scanner input = new Scanner( System.in );
int totalSales;
int salesPlusSalary;

//input commissions
System.out.print( "Enter Annual Commissions" );
totalSales = input.nextInt();

salesPlusSalary = (int) (totalSales * .05 + SalesBaseSalary);

System.out.printf( "Sales commission plus Salary is %d\n", salesPlusSalary );
}
}

现在,如果我将其作为一个类来执行,它会完美地工作......但我必须有两个。这是单个类。

package salescompensation;

//Adding Scanner Utility
import java.util.Scanner;

//Main Class representing the employees base salary
public class SalesCompensation {

public static void main(String[] args) {
//Scanner input
Scanner input = new Scanner( System.in );
int totalSales;
int salesPlusSalary;
//base salary
double baseSalary = 75000;
//input commissions
System.out.print( "Enter Annual Commissions" );
totalSales = input.nextInt();

salesPlusSalary = (int) (totalSales * .05 + baseSalary);

System.out.printf( "Sales commission plus Salary is %d\n", salesPlusSalary );

}

}

我试图找出我做错了什么以及为什么做错了,这样我就可以学习。

最佳答案

要扩展 sheltem 的答案,您应该从 SalesCommission 类的实例获取该字段(假设它不是类常量)。

public static void main(String[] args) 
{
// Create an instance of the SalesCommission class (an object)
SalesCommission salesCommission = new SalesCommission();

//Scanner input
Scanner input = new Scanner( System.in );
int totalSales;
int salesPlusSalary;

//input commissions
System.out.print( "Enter Annual Commissions" );
totalSales = input.nextInt();

salesPlusSalary = (int) (totalSales * .05 + SalesCommission.baseSalary);

System.out.printf( "Sales commission plus Salary is %d\n", salesPlusSalary );
}

此外,由于您刚刚掌握 Java、OOP 和类等知识,我建议您阅读 The Java Tutorials或其他类似的资源,并审查您的设计以及如何改进它。通过努力提高,您将学习 OOP 和良好的设计/实践。

关于使用继承时会出现 Java 操作数错误,但在单个类上执行相同操作时不会出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21690862/

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