本文主要是介绍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---抽象类练习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!