wordpress 网站备案号,做网站按什么收费多少钱,免费域名空间哪个好,青海企业网站制作练习一
编写工资系统#xff0c;实现不同类型员工(多态)的按月发放工资。如果当月出现某个Employee对象的生日#xff0c;则将该雇员的工资增加100元。实验说明#xff1a;#xff08;1#xff09;定义一个Employee类#xff0c;该类包含#xff1a;private成员变量na…练习一
编写工资系统实现不同类型员工(多态)的按月发放工资。如果当月出现某个Employee对象的生日则将该雇员的工资增加100元。实验说明1定义一个Employee类该类包含private成员变量name,number,birthday其中birthday 为MyDate类的对象
提供必要的构造器
abstract方法earnings(),返回工资数额
toString()方法输出对象的name,number和birthday。2MyDate类包含:
private成员变量year,month,day
提供必要的构造器
toDateString()方法返回日期对应的字符串xxxx年xx月xx日3定义SalariedEmployee类继承Employee类实现按月计算工资的员工处理。
该类包括private成员变量monthlySalary
提供必要的构造器
实现父类的抽象方法earnings(),该方法返回monthlySalary值
toString()方法输出员工类型信息及员工的namenumber,birthday。比如SalariedEmployee[name ,number ,birthdayxxxx年xx月xx日]4参照SalariedEmployee类定义HourlyEmployee类实现按小时计算工资的员工处理。该类包括
private成员变量wage和hour
提供必要的构造器
实现父类的抽象方法earnings(),该方法返回wage*hour值
toString()方法输出员工类型信息及员工的namenumber,birthday。5定义PayrollSystem类创建Employee变量数组并初始化该数组存放各类雇员对象的引用。
利用循环结构遍历数组元素输出各个对象的类型,name,number,birthday,以及该对象生日。
当键盘输入本月月份值时如果本月是某个Employee对象的生日还要输出增加工资信息。//提示
//定义People类型的数组People c1[]new People[10];
//数组元素赋值
c1[0]new People(John,0001,20);
c1[1]new People(Bob,0002,19);
//若People有两个子类Student和Officer则数组元素赋值时可以使父类类型的数组元素指向子类。
c1[0]new Student(John,0001,20,85.0);
c1[1]new Officer(Bob,0002,19,90.5); package chapter08_oop3.src.com.atguigu07._abstract.exer2;/*** ClassName: Employee* Package: chapter08_oop3.src.com.atguigu07._abstract.exer2* Description:** 1定义一个Employee类该类包含** private成员变量name,number,birthday其中birthday 为MyDate类的对象* 提供必要的构造器* abstract方法earnings(),返回工资数额* toString()方法输出对象的name,number和birthday。** Author 小白* Create 2024/4/4 12:48* Version 1.0*/
public abstract class Employee {private String name;private int number;private MyDate birthday;public Employee() {}public Employee(String name, int number, MyDate birthday) {this.name name;this.number number;this.birthday birthday;}public String getName() {return name;}public void setName(String name) {this.name name;}public int getNumber() {return number;}public void setNumber(int number) {this.number number;}public MyDate getBirthday() {return birthday;}public void setBirthday(MyDate birthday) {this.birthday birthday;}public abstract double earnings();public String toString(){return name name ,number number , birthday birthday.toDateString();}}package chapter08_oop3.src.com.atguigu07._abstract.exer2;import chapter08_oop3_teacher.src.com.atguigu07._abstract.exer2.Employee;
import chapter08_oop3_teacher.src.com.atguigu07._abstract.exer2.MyDate;/*** ClassName: HourlyEmployee* Description:* 参照SalariedEmployee类定义HourlyEmployee类实现按小时计算工资的员工处理。该类包括* private成员变量wage和hour* 提供必要的构造器* 实现父类的抽象方法earnings(),该方法返回wage*hour值* toString()方法输出员工类型信息及员工的namenumber,birthday。* Author 尚硅谷-宋红康* Create 15:45* Version 1.0*/
public class HourlyEmployee extends Employee {private double wage;//单位小时的工资private int hour;//月工作的小时数public HourlyEmployee() {}public HourlyEmployee(String name, int number, MyDate birthday, double wage, int hour) {super(name, number, birthday);this.wage wage;this.hour hour;}public double getWage() {return wage;}public void setWage(double wage) {this.wage wage;}public int getHour() {return hour;}public void setHour(int hour) {this.hour hour;}Overridepublic double earnings() {return wage * hour;}public String toString(){return HourlyEmployee[ super.toString() ];}
}package chapter08_oop3.src.com.atguigu07._abstract.exer2;/*** ClassName: MyDate* Package: chapter08_oop3.src.com.atguigu07._abstract.exer2* Description:** 2MyDate类包含:* private成员变量year,month,day* 提供必要的构造器* toDateString()方法返回日期对应的字符串xxxx年xx月xx日** Author 小白* Create 2024/4/4 12:49* Version 1.0*/
public class MyDate {private int year;private int month;private int day;public MyDate() {}public MyDate(int year, int month, int day) {this.year year;this.month month;this.day day;}public int getYear() {return year;}public void setYear(int year) {this.year year;}public int getMonth() {return month;}public void setMonth(int month) {this.month month;}public int getDay() {return day;}public void setDay(int day) {this.day day;}public String toDateString() {return year 年 month 月 day 日;}
}package chapter08_oop3.src.com.atguigu07._abstract.exer2;import chapter08_oop3_teacher.src.com.atguigu07._abstract.exer2.Employee;
import chapter08_oop3_teacher.src.com.atguigu07._abstract.exer2.HourlyEmployee;
import chapter08_oop3_teacher.src.com.atguigu07._abstract.exer2.MyDate;
import chapter08_oop3_teacher.src.com.atguigu07._abstract.exer2.SalariedEmployee;import java.util.Scanner;/*** ClassName: PayrollSystem* Description:* 定义PayrollSystem类创建Employee变量数组并初始化该数组存放各类雇员对象的引用。* 利用循环结构遍历数组元素输出各个对象的类型,name,number,birthday。* 当键盘输入本月月份值时如果本月是某个Employee对象的生日还要输出增加工资信息。** Author 尚硅谷-宋红康* Create 15:47* Version 1.0*/
public class PayrollSystem {public static void main(String[] args) {Scanner scan new Scanner(System.in);chapter08_oop3_teacher.src.com.atguigu07._abstract.exer2.Employee[] emps new Employee[2];emps[0] new SalariedEmployee(张小亮,1001,new chapter08_oop3_teacher.src.com.atguigu07._abstract.exer2.MyDate(1992,12,21),18000);emps[1] new HourlyEmployee(侯少鹏,1002,new MyDate(1997,11,12),240,100);System.out.println(请输入当前的月份);int month scan.nextInt();for (int i 0; i emps.length; i) {System.out.println(emps[i].toString());System.out.println(工资为 emps[i].earnings());if(month emps[i].getBirthday().getMonth()){System.out.println(生日快乐加薪100);}}scan.close();}
}package chapter08_oop3.src.com.atguigu07._abstract.exer2;/*** ClassName: SalariedEmployee* Package: chapter08_oop3.src.com.atguigu07._abstract.exer2* Description:** 3定义SalariedEmployee类继承Employee类实现按月计算工资的员工处理。* 该类包括private成员变量monthlySalary* 提供必要的构造器* 实现父类的抽象方法earnings(),该方法返回monthlySalary值* toString()方法输出员工类型信息及员工的namenumber,birthday。比如SalariedEmployee[name ,number ,birthdayxxxx年xx月xx日]** Author 小白* Create 2024/4/4 13:19* Version 1.0*/
public class SalariedEmployee extends Employee{private double monthlySalary; //月工资public SalariedEmployee() {}Overridepublic double earnings() {return monthlySalary;}public SalariedEmployee(String name, int number, MyDate birthday, double monthlySalary) {super(name, number, birthday);this.monthlySalary monthlySalary;}// public double getMonthlySalary() {
// return monthlySalary;
// }public void setMonthlySalary(double monthlySalary) {this.monthlySalary monthlySalary;}public String toString(){return SalariedEmployee[ super.toString() ];}
}