java 员工类 成员变量 空构造方法 初始化所有成员变量的构造方法
定义员工类
定义员工类Employee
-
成员变量:职工号、姓名、性别、工龄、基本工资、交通补贴、午餐补助、奖金
-
定义一个空构造方法和一个能够初始化所有成员变量的构造方法。
-
定义统计实发工资总额的方法
-
利用toString()方法返回员工的所有信息
-
定义方法输出职工号、姓名、工龄、实发工资。
题解
class Employee {
int id;
String name;
String sex;
int workAge;
int basePay;
int transportation;
int lunchSubsidies;
int bonuses;
Employee(int id, String name, String sex, int workAge, int basePay, int transportation, int lunchSubsidies, int bonuses) {
this.id = id;
this.name = name;
this.sex = sex;
this.workAge = workAge;
this.basePay = basePay;
this.transportation = transportation;
this.lunchSubsidies = lunchSubsidies;
this.bonuses = bonuses;
}
Employee() {
}
int factPay(){
return basePay + transportation + lunchSubsidies + bonuses;
}
@Override
public String toString(){
return "职工号:"+ id +" 姓名:"+ name+" 性别:"+sex +" 工龄:"+workAge +" 基本工资:"+basePay +
" 交通补贴:"+ transportation+" 午餐补助:"+lunchSubsidies +" 奖金:"+bonuses;
}
public String toDital(){
return "职工号:"+ id +" 姓名:"+ name+" 性别:"+sex +" 工龄:"+workAge +" 实发工资"+factPay();
}
}
public class Empl02 {
//职工号"+ +"姓名"+ +"性别"+ +"工龄"+ +"基本工资"+ +"交通补贴"+ +"午餐补助"+ +"奖金
public static void main(String[] args) {
Employee e0 = new Employee();
e0.id = 12;
e0.name = "A";
e0.sex = "女";
e0.workAge = 5600;
e0.basePay = 689;
e0.transportation = 476;
e0.lunchSubsidies = 389;
e0.bonuses = 600;
System.out.println(e0.toString() +"\n"+ e0.toDital());
Employee e1 = new Employee(11,"B","男",8,5000,500,90,46);
System.out.println(e1.toString() +"\n"+ e1.toDital());
}
}
运行结果
职工号:12 姓名:A 性别:女 工龄:5600 基本工资:689 交通补贴:476 午餐补助:389 奖金:600
职工号:12 姓名:A 性别:女 工龄:5600 实发工资2154
职工号:11 姓名:B 性别:男 工龄:8 基本工资:5000 交通补贴:500 午餐补助:90 奖金:46
职工号:11 姓名:B 性别:男 工龄:8 实发工资5636

浙公网安备 33010602011771号