运用继承来给员工发工资

来源:互联网 发布:软件自动升级程序 编辑:程序博客网 时间:2024/04/30 09:55

package com.yh.javaoo006.exe1;
//创建出一个类Employee
 //其有三个子类:小时工、月薪工、年薪工
 //其都有姓名、性别、工资属性;计算工资行为
 //年薪工有分红行为
 //月薪工有打卡行为
 //自行创建出类,并进行自转和强转练习
//创建一个老板类
 //该类仅有发工资行为
public class Exe1 {
 public static void main(String[] args) {
  Hour h = new Hour("小明", "男", 35);
  Year year = new Year("小强", "男", 200000, 200000);
  
  Boss b = new Boss();
  b.faGongZi(year);
 }
}

____________________________________________________________________________________

package com.yh.javaoo006.exe1;



public class Employee {
 private String name;//姓名
 private String gender;//性别
 private int money;//工资
 
 public Employee() {
  // TODO Auto-generated constructor stub
 }
 public Employee(String name, String gender, int money) {
  super();
  this.name = name;
  this.gender = gender;
  this.money = money;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getGender() {
  return gender;
 }
 public void setGender(String gender) {
  this.gender = gender;
 }
 public int getMoney() {
  return money;
 }
 public void setMoney(int money) {
  this.money = money;
 }
 @Override
 public String toString() {
  return "Employee [name=" + name + ", gender=" + gender + ", money=" + money + "]";
 }
 


 public int computeMoney() {//计算工资的行为
  return 0;
 }
 
}

__________________________________________________________________________

package com.yh.javaoo006.exe1;
public class Boss {
 //如果是年薪工
 //Employee e = 年薪工对象的地址
 public void faGongZi(Employee e) {
  //如何区分不同子类的对象
  //使用instanceof关键字判断该对象是否是某一个类的对象
  //用法e instanceof Hour  e是对象  Hour是类
//  if(e instanceof Hour) {
//   Hour h = (Hour) e;
//   System.out.println(h.computeMoney());
//  }else if(e instanceof Month) {
//   Month h = (Month) e;
//   System.out.println(h.computeMoney());
//  }else if(e instanceof Year) {
//   Year y = (Year)e;
//   System.out.println(y.computeMoney()+y.fenHong());
//  }
  //如果子类和父类的行为都一致
  //可以直接使用动态多态的效果来简化过程
  System.out.println(e.computeMoney());
 }
}
_____________________________________________________________
//小时工
public class Hour extends Employee{
 
 public Hour() {
  // TODO Auto-generated constructor stub
 }
 
 public Hour(String name,String gender,int money) {
  super(name,gender,money);
 }
 @Override
 public int computeMoney() {
  
  return this.getMoney()*8*28;
 }
 
}
______________________________________________________
public class Month extends Employee{
 public Month() {
  // TODO Auto-generated constructor stub
 }
 
 public Month(String name,String gender,int money) {
  super(name,gender,money);
 }
 @Override
 public int computeMoney() {
  
  return this.getMoney();
 }
 
 public void daKa() {//打卡
  System.out.println(this.getName()+"打卡");
 }
 
}
_______________________________________________________
public class Year extends Employee{
 
 private int fenHong;
 
 public Year() {
  // TODO Auto-generated constructor stub
 }
 
 public Year(String name,String gender,int money,int fenHong) {
  super(name,gender,money);
  this.fenHong = fenHong;
 }
 
 
 
 public int getFenHong() {
  return fenHong;
 }
 public void setFenHong(int fenHong) {
  this.fenHong = fenHong;
 }
 public int fenHong() {
  return this.fenHong/12;
 }
 @Override
 public int computeMoney() {
  return this.getMoney()/12+fenHong();
 }
 @Override
 public String toString() {
  super.toString();
  return "Year [fenHong=" + fenHong + "]";
 }
 
 
 
 
}

阅读全文
0 0