编程语言
首页 > 编程语言> > JAVA---抽象类练习

JAVA---抽象类练习

作者:互联网

package exer;

import java.util.Scanner;

public class PayrollSystem {
	public static void main(String[] args) {
		Scanner scan=new Scanner(System.in);
		System.out.println("请输入当月的月份");
		int month=scan.nextInt();
		
		Employee[] emps=new Employee[2];
		emps[0]=new SalariedEmployee("张三",1,new MyDate(1999,2,2),10000);
		emps[1]=new HourlyEmployee("李四",2,new MyDate(1111,2,2),100,100);
		
		for(int i=0;i<emps.length;i++){
			System.out.println(emps[i]);
			double salary=emps[i].earnings();
			System.out.println("月工资为:"+salary);
			if((month)==emps[i].getBirthday().getMonth()){
				System.out.println("生日快乐!奖励100元");
			}
		}
	}
}
class MyDate{
	private int year;
	private int month;
	private int day;
	
	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+"日";
	}
}
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();
	}
}
class SalariedEmployee extends Employee{
	private double monthlySalary;
	public SalariedEmployee(){
		
	}
	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;
	}
	
	public double earnings(){
		return monthlySalary;
	}
	public String toString(){
		return "SalariedEmployee["+super.toString()+"]";
	}
}
class HourlyEmployee extends Employee{
	private int wage;
	private int hour;
	
	public HourlyEmployee(){
		
	}
	public HourlyEmployee(String name,int number,MyDate birthday){
		super(name,number,birthday);
	}
	public HourlyEmployee(String name,int number,MyDate birthday,int wage,int hour){
		super(name,number,birthday);
		this.wage=wage;
		this.hour=hour;
	}
	public int getWage(){
		return wage;
	}
	public void setWage(int wage){
		this.wage=wage;
	}
	public int getHour(){
		return hour;
	}
	public void setHour(int hour){
		this.hour=hour;
	}
	public double earnings(){
		return wage*hour;
	}
	public String toString(){
		return "HourlyEmployee["+super.toString()+"]";
	}
}

标签:JAVA,Scanner,scan,int,System,---,emps,new,抽象类
来源: https://www.cnblogs.com/fate-/p/15849350.html