JAVA---抽象类练习
2022/1/27 12:04:15
本文主要是介绍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---抽象类练习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-05-15鸿蒙生态设备数量超8亿台
- 2024-05-13TiDB + ES:转转业财系统亿级数据存储优化实践
- 2024-05-09“2024鸿蒙零基础快速实战-仿抖音App开发(ArkTS版)”实战课程已上线
- 2024-05-09聊聊如何通过arthas-tunnel-server来远程管理所有需要arthas监控的应用
- 2024-05-09log4j2这么配就对了
- 2024-05-09nginx修改Content-Type
- 2024-05-09Redis多数据源,看这篇就够了
- 2024-05-09Google Chrome驱动程序 124.0.6367.62(正式版本)去哪下载?
- 2024-05-09有没有大佬知道这种数据应该怎么抓取呀?
- 2024-05-09这种运行结果里的10.100000001,怎么能最快改成10.1?