抽象类的实例应用

1.员工

package bigguy;

/**
* @author liu$
* @version 1.0
* @description: TODO
* @date $ $
*/
public abstract class Employee {
private String name ;
private int number ;
private MyDate birthday ;

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( ) ;

@Override
public String toString() {
return "name='" + name + '\'' +
", number=" + number +
", birthday=" + birthday.toDateString() +
'}';
}


}
2.生日
package bigguy;

/**
* @author liu$
* @version 1.0
* @description: TODO
* @date $ $
*/
public 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 "MyDate{" +
"year=" + year +
", month=" + month +
", day=" + day +
'}';
}
}
3.普通月薪员工
package bigguy;

/**
* @author liu$
* @version 1.0
* @description: TODO
* @date $ $
*/
public class SalariedEmployee extends Employee {
private double mothlysalary ;
public SalariedEmployee(String name, int number, MyDate birthday) {
super(name, number, birthday);
}
public SalariedEmployee(String name, int number, MyDate birthday,double mothlysalary) {

super(name, number, birthday);
this.mothlysalary = mothlysalary;
}

public double getMothlysalary() {
return mothlysalary;
}

public double earnings() {

return mothlysalary ;
}

public String toString() {
return "SalariedEmployee{" +super.toString() +"]" ;
}
}
4.小时工
package bigguy;

/**
* @author liu$
* @version 1.0
* @description: TODO
* @date $ $
*/
public class HourlyEmployee extends Employee {
private int wage;//每小时工资
private int hour;//一个月工作了多少小时

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;
}

@Override
public double earnings() {
return wage * hour;
}
public String toString() {
return "HourlyEmployee{" + super.toString() + "]";
}
}
5.总薪水(生日+100)
package bigguy;

import java.util.Scanner;

/**
* @author liu$
* @version 1.0
* @description: TODO
* @date $ $
*/
public class PayrollSystem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in) ;
System.out.println("请输入当月的月份:");
int month = scanner.nextInt() ;
Employee[] emps = new Employee[2] ;
emps[0] = new SalariedEmployee("大嘴盈",1001,new MyDate(2003,9,28),5000) ;
emps[1] = new HourlyEmployee("柳",1002,new MyDate(2003,8,10),240,60) ;

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块");
}
}
}
}
posted @   平凡的柳先生  阅读(53)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
点击右上角即可分享
微信分享提示