学java的第十四天

编程入门 行业动态 更新时间:2024-10-18 03:27:06

学java的<a href=https://www.elefans.com/category/jswz/34/1708283.html style=第十四天"/>

学java的第十四天

今天其实就做了一个关于接口i的练习


/** 编写工资系统,实现不同类型员工(多态)的按月发放工资。如果当月出现某个
Employee对象的生日,则将该雇员的工资增加100元。
实验说明:
(1)定义一个Employee类,该类包含:
private成员变量name,number,birthday,其中birthday 为MyDate类的对象;
abstract方法earnings();
toString()方法输出对象的name,number和birthday。 (2)MyDate类包含:
private成员变量year,month,day ;
toDateString()方法返回日期对应的字符串:xxxx年xx月xx日 (3)定义SalariedEmployee类继承Employee类,实现按月计算工资的员工处
理。该类包括:private成员变量monthlySalary;
实现父类的抽象方法earnings(),该方法返回monthlySalary值;toString()方法输
出员工类型信息及员工的name,number,birthday。
(4)参照SalariedEmployee类定义HourlyEmployee类,实现按小时计算工资的
员工处理。该类包括:
private成员变量wage和hour;
实现父类的抽象方法earnings(),该方法返回wage*hour值;
toString()方法输出员工类型信息及员工的name,number,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);* * * */
public abstract class Employee {private String name;private int number;private MyDate birthday;public abstract double earnings();public Employee() {super();}public Employee(String name, int number, MyDate birthday) {super();this.name = name;this.number = number;this.birthday = birthday;}@Overridepublic String toString() {return "Employee [name=" + name + ", number=" + number + ", 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 String getBirthday() {return birthday.toDateString();}public MyDate getBirthdayElement() {return birthday;}public void setBirthday(MyDate birthday) {this.birthday = birthday;}}class MyDate
{private int year;private int month;private int day;public MyDate() {super();}public MyDate(int year, int month, int day) {super();this.year = year;this.month = month;this.day = day;}public int getMonth() {return month;}public String toDateString() {return year+"年"+month+"月"+day+"日";}}/** (3)定义SalariedEmployee类继承Employee类,实现按月计算工资的员工处
理。该类包括:private成员变量monthlySalary;
实现父类的抽象方法earnings(),该方法返回monthlySalary值;toString()方法输
出员工类型信息及员工的name,number,birthday。* */
class SalariedEmployee extends Employee
{private double monthSalary;public SalariedEmployee() {super();}public SalariedEmployee(String name, int number, MyDate birthday, double monthSalary) {super(name, number, birthday);this.monthSalary = monthSalary;}@Overridepublic double earnings() {return monthSalary;}@Overridepublic String toString() {return "Employee [name=" + this.getName() + ", number=" + this.getNumber() + ", birthday=" + this.getBirthday() + "]";}}/** (4)参照SalariedEmployee类定义HourlyEmployee类,实现按小时计算工资的
员工处理。该类包括:
private成员变量wage和hour;
实现父类的抽象方法earnings(),该方法返回wage*hour值;
toString()方法输出员工类型信息及员工的name,number,birthday。 * */class HourlyEmployee extends Employee
{private double wage;private double hour;public HourlyEmployee() {super();}public HourlyEmployee(String name, int number, MyDate birthday, double wage, double hour) {super(name, number, birthday);this.wage = wage;this.hour = hour;}@Overridepublic double earnings() {return wage*hour;}@Overridepublic String toString() {return "Employee [name=" + this.getName() + ", number=" + this.getNumber() + ", birthday=" + this.getBirthday() + "]";}}

上面是接口和父类

下面是测试类

import java.util.Calendar;/** /** (5)定义PayrollSystem类,创建Employee变量数组并初始化,该数组存放各
类雇员对象的引用。利用循环结构遍历数组元素,输出各个对象的类
型,name,number,birthday,以及该对象生日。当键盘输入本月月份值时,如果本
月是某个Employee对象的生日,还要输出增加工资信息。* */public class PayrollSystem {public static void main(String[] args) {Calendar calendar =Calendar.getInstance();int month = calendar.get(Calendar.MONTH);month++;//Scanner scan=new Scanner(System.in);Employee[] employees=new Employee[] {new SalariedEmployee("zwh", 1, new MyDate(1998,6, 26), 12000),new HourlyEmployee("涂安丽", 2, new MyDate(1997, 3, 1), 100, 10)};for (int i = 0; i < employees.length; i++) {System.out.println(employees[i].toString());System.out.println("当前月份为"+month);//System.out.println("请输入当前月份");//int month = scan.nextInt();System.out.println("你的生日是"+employees[i].getBirthdayElement().getMonth());if (month==employees[i].getBirthdayElement().getMonth()) {System.out.println("你的工资是"+employees[i].earnings()+"加上你的生日奖"+100+"合计"+(employees[i].earnings()+100));}else {System.out.println("你的工资是"+employees[i].earnings());}}}}

其实没多难,自己写完再听老师讲就很有感觉

对了,我还差到了那个calendar中get的month是从0开始计算的,所以用的时候要加一

今宵剩把银缸照,犹恐相逢是梦中。

更多推荐

学java的第十四天

本文发布于:2024-03-05 10:07:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1712002.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:第十四天   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!