Java中的小练习,关于继承,多态,重写,abstract

Employee类

package com.my.exer1;
/**
 * 
 * @ClassName: Employee
 * @Description: 定义一个Employee类,该类包含:
 * private成员变量name,number,birthday,其中birthday 为MyDate类的对象;
 * abstract方法earnings();
 * toString()方法输出对象的name,number和birthday。
 * @author Redamancy
 * @date 2022-07-02 01:21:19
 */
public abstract class Employee {
	private String name;
	private int number;
	private MyDate birthday;
	
	
	
//	public Employee() {
//		super();
//	}


	public Employee(String name, int number, MyDate birthday) {
		super();
		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();
	
	
	@Override
	public String toString() {
		return "name=" + name + ", number=" + number + ", birthday=" + birthday;
	}
}

MyDate类

package com.my.exer1;
/**
 * 
 * @ClassName: MyDate
 * @Description: MyDate类包含:
 * private成员变量year,month,day ;
 * toDateString()方法返回日期对应的字符串:xxxx年xx月xx日
 * @author Redamancy
 * @date 2022-07-02 01:21:56
 */
public class MyDate {
	private int year;
	private int month;
	private int day;
	
	
	public MyDate(int year, int month, int day) {
		super();
		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 + "天";
	}
	
	
	
}

SalariedEmployee类

package com.my.exer1;
/**
 * 
 * @ClassName: SalariedEmployee
 * @Description: 定义SalariedEmployee类继承Employee类,实现按月计算工资的员工处理。
 * 该类包括:private成员变量monthlySalary;
 * 实现父类的抽象方法earnings(),该方法返回monthlySalary值;
 * toString()方法输出员工类型信息及员工的name,number,birthday。
 * @author Redamancy
 * @date 2022-07-02 01:22:10
 */
public class SalariedEmployee extends Employee{
	
	private double monthlySalary;
	
	public SalariedEmployee(String name, int number, MyDate birthday) {
		super(name, number, birthday);
	}
	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;
	}

	@Override
	public double earnings() {
		// TODO Auto-generated method stub
		return monthlySalary;
	}

	@Override
	public String toString() {
		return "SalariedEmployee [" + super.toString() + "]";
	}
	
	

	
	
	
}

HourlyEmployee类

package com.my.exer1;
/**
 * 
 * @ClassName: HourlyEmployee
 * @Description: 参照SalariedEmployee类定义HourlyEmployee类,实现按小时计算工资的员工处理。该类包括:
 * private成员变量wage和hour;
 * 实现父类的抽象方法earnings(),该方法返回wage*hour值;
 * toString()方法输出员工类型信息及员工的name,number,birthday
 * @author Redamancy
 * @date 2022-07-02 01:37:30
 */
public class HourlyEmployee extends Employee{
	private double wage;//每小时的工资
	private int hour;//月工作的小时数
	
	
	public HourlyEmployee(String name, int number, MyDate birthday) {
		super(name, number, birthday);
		// TODO Auto-generated constructor stub
	}
	public HourlyEmployee(String name, int number, MyDate birthday, double wage, int hour) {
		super(name, number, birthday);
		this.wage = wage;
		this.hour = hour;
		// TODO Auto-generated constructor stub
	}
	
	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;
	}
	
	@Override
	public double earnings() {
		// TODO Auto-generated method stub
		return wage * hour;
	}

	@Override
	public String toString() {
		return "HourlyEmployee [" + super.toString() + "]";
	}
	
}

PayrollSystem类

package com.my.exer1;

import java.util.Calendar;
import java.util.Scanner;

/**
 * 
 * @ClassName: PayrollSystem
 * @Description: 定义PayrollSystem类,创建Employee变量数组并初始化,该数组存放各类雇员对象的引用。
 * 利用循环结构遍历数组元素,输出各个对象的类型,name,number,birthday。
 * 当键盘输入本月月份值时,如果本月是某个Employee对象的生日,还要输出增加工资信息。
 * @author Redamancy
 * @date 2022-07-02 01:43:10
 */
public class PayrollSystem {
	public static void main(String[] args) {
		//方式一:
//		Scanner scanner = new Scanner(System.in);
//		System.out.println("请输入当年的月份:");
//		int month = scanner.nextInt();
		
//		方式二:
		Calendar calendar = Calendar.getInstance();
		int month = calendar.get(Calendar.MONTH);
		System.out.println(month);//一月份为0
		
		Employee[] emps= new Employee[2]; //因为只是创了一个数组,Employee类型的,所以是可以这样写的
		
		emps[0] = new SalariedEmployee("萨摩耶", 1001, new MyDate(2022, 2, 10), 5000);
		emps[1] = new HourlyEmployee("金毛", 1002, new MyDate(2021, 1, 10), 50, 140);
		
		for(int i = 0; i < emps.length; i++) {
			System.out.println(emps[i]);
			double salary = emps[i].earnings();
			System.out.println("月工资为:" + salary);
			
			if((month + 1) == emps[i].getBirthday().getMonth()) {
				System.out.println("生日快乐!奖励100元!");
				
			}
			
		}
		
	}
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Redamancy_06

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值