某公司的员工分为5类,每类员工都有相应的封装类,这5个类的信息如下
(1)Employee:这是所有员工的父类。
①属性:员工的姓名、员工的生日月份。
)方法:getSalary(int month)根据参数月份确定工资。如果该月员工过生日,则公司会额外发放100元。
(2)SalariedEmployee:Employee 的子类,拿固定工资的员工。
属性:月薪。
(3)HourlyEmployee:Employee的子类,按小时拿工资的员工,每月工作超出160h的
部分按照1.5倍工资发放。
属性:每小时的工资、每月工作的小时数。
(4)SalesEmployee:Employee的子类,销售人员,工资由月销售额和提成率决定。
属性:月销售额、提成率。
(5)BasePlusSalesEmployee:SalesEmployee的子类,有固定底薪的销售人员,工资为
底薪加上销售提成。
属性:底薪。
本题要求根据上述员工分类,编写一个程序,实现以下功能:
(1)创建一个Employee数组,分别创建若干不同的Employee对象,并打印某个月的
(2)每个类都完全封装,不允许有非私有化属性
package jquery;
class Employee
{
private String name; //员工姓名
private int month; //员工生日月份
public Employee(){}
public Employee(String name,int month){
this.name = name;
this.month = month;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setMonth(int month){
this.month = month;
}
public int getMonth(){
return month;
}
public double getSalary(int month){
double salary = 0;
if(this.month == month){
salary +=100;
}
return salary;
}
}
class SalariedEmployee extends Employee
{
private double monthsalary;//月薪
public SalariedEmployee(){}
public SalariedEmployee(String name,int month,double monthsalary){
super(name,month);
this.monthsalary = monthsalary;
}
public void setMonthsalary(double monthsalary){
this.monthsalary = monthsalary;
}
public double getMonthsalary(){
return monthsalary;
}
public double getSalary(int month){
double salary = monthsalary+super.getSalary(month);
return salary;
}
class HourlyEmployee extends Employee
{
private double hourlysalary; //每小时的工资
private int counthour; //每月工作的小时数
public HourlyEmployee(){}
public HourlyEmployee(String name, int month, double hourlysalary, int counthour){
super(name, month);
this.hourlysalary = hourlysalary;
this.counthour = counthour;
}
public void setHourlysalary(double hourlysalary) {
this.hourlysalary = hourlysalary;
}
public double getHourlysalary() {
return hourlysalary;
}
public void setCounthour(int counthour) {
this.counthour = counthour;
}
public int getCounthour() {
return counthour;
}
@Override
public double getSalary(int month) {
double salary = 0;
if (counthour > 160) {
salary = 160*hourlysalary+(counthour-160)*hourlysalary*3/2;
}else{
salary = hourlysalary*counthour+super.getSalary(month);
}
return salary+super.getSalary(month);
}
}
}
class SalesEmployee extends Employee
{
private double monthlysales; //月销售额
private double commissiorate; //提成率
public SalesEmployee(){}
public SalesEmployee(String name,int month,double monthlysales,double commissiorate){
super(name,month);
this.monthlysales = monthlysales;
this.commissiorate = commissiorate;
}
public void setMonthlysales(double monthlysales) {
this.monthlysales = monthlysales;
}
public double getMonthlysales() {
return monthlysales;
}
public void setCommissiorate(double commissiorate) {
this.commissiorate = commissiorate;
}
public double getCommissiorate() {
return commissiorate;
}
public double getSalary(int month){
double salary = 0;
return salary = super.getSalary(month)+monthlysales*(1+commissiorate);
}
}
class BasePlusSalesEmployee extends SalesEmployee
{
private double basesalary; //底薪
public BasePlusSalesEmployee(){}
public BasePlusSalesEmployee(String name,int month,double monthlysales,double commissiorate,double basesalary){
super(name,month,monthlysales,commissiorate);
this.basesalary = basesalary;
}
public void setBasesalary(double basesalary) {
this.basesalary = basesalary;
}
public double getBasesalary() {
return basesalary;
}
public double getSalary(int month) {
double salary = 0;
return salary = basesalary+super.getSalary(month);
}
}
public class Test1
{
public static void main(String[] args){
Employee []employee = { new SalariedEmployee("张三",4,4000),
new HourlyEmployee("黄四",12,60,39),
new SalesEmployee("李武",9,355,0.2),
new BasePlusSalesEmployee("秦见",10,242,0.2,1000)
};
System.out.println("十月份工资:");
for (int i = 0; i < employee.length; i++) {
System.out.println("姓名:" + employee[i].getName() + " 生日月份:" + employee[i].getMonth() + " 工资为:" + employee[i].getSalary(10));
}
}
}
java: 找不到符号
符号: 类 HourlyEmployee
位置: 类 jquery.Test1
一直报这个错,求各位兄弟姐妹帮解答
该回答通过自己思路及引用到GPTᴼᴾᴱᴺᴬᴵ搜索,得到内容具体如下:
在代码中,HourlyEmployee类的定义中缺少一个右大括号(}),导致该类定义没有关闭,从而影响了后续代码的编译。
请将代码中HourlyEmployee类的定义修改为如下形式即可:
class HourlyEmployee extends Employee {
private double hourlysalary; //每小时的工资
private int counthour; //每月工作的小时数
public HourlyEmployee() {}
public HourlyEmployee(String name, int month, double hourlysalary, int counthour) {
super(name, month);
this.hourlysalary = hourlysalary;
this.counthour = counthour;
}
public void setHourlysalary(double hourlysalary) {
this.hourlysalary = hourlysalary;
}
public double getHourlysalary() {
return hourlysalary;
}
public void setCounthour(int counthour) {
this.counthour = counthour;
}
public int getCounthour() {
return counthour;
}
@Override
public double getSalary(int month) {
double salary = 0;
if (counthour > 160) {
salary = 160 * hourlysalary + (counthour - 160) * hourlysalary * 3 / 2;
} else {
salary = hourlysalary * counthour + super.getSalary(month);
}
return salary + super.getSalary(month);
}
}
此外,建议在类定义结束后,使用空行进行分隔,以提高代码的可读性。
如果以上回答对您有所帮助,点击一下采纳该答案~谢谢
换成静态的,因为你这个类载SalariedEmployee里面呢
static class HourlyEmployee extends Employee
HourlyEmployee 你放到了 SalariedEmployee里面,就是这个SalariedEmployee类的内部类,主函数 就不饿能直接调用了,
解决:
你把 HourlyEmployee 放到 和 SalariedEmployee 同级
或者 HourlyEmployee 定义成 static 修饰,然后这样调用
new SalariedEmployee.HourlyEmployee("黄四",12,60,39)