文章来源:https://www.toymoban.com/news/detail-756573.html
文章来源地址https://www.toymoban.com/news/detail-756573.html
1.Employee.java
package Message;
import java.util.Date;
class Employee {
private String name;
private int birthMonth;
public Employee(String name, int birthMonth) {
this.name = name;
this.birthMonth = birthMonth;
}
public double getSalary(int month) {
double salary = 0.0; // 假设基本工资为5000元
salary = 5000.0;
if (birthMonth == month) {
salary += 100.0;
}
return salary;
}
}
2.SalariedEmployee.java
package Message;
class SalariedEmployee extends Employee {
private double monthlySalary;
public SalariedEmployee(String name, int birthMonth, double monthlySalary) {
super(name, birthMonth);
this.monthlySalary = monthlySalary;
}
public double getMonthlySalary() {
return monthlySalary;
}
public double getSalary(int month) {
return super.getSalary(month) + monthlySalary;
}
}
3.HourlyEmployee.java
package Message;
class HourlyEmployee extends Employee {
private double hourlyWage;
private int monthlyHoursWorked;
public HourlyEmployee(String name, int birthMonth, double hourlyWage, int monthlyHoursWorked) {
super(name, birthMonth);
this.hourlyWage = hourlyWage;
this.monthlyHoursWorked = monthlyHoursWorked;
}
public double getHourlyWage() {
return hourlyWage;
}
public int getMonthlyHoursWorked() {
return monthlyHoursWorked;
}
public double getSalary(int month) {
double baseSalary = super.getSalary(month);
double overtimePay = (monthlyHoursWorked > 160) ? (monthlyHoursWorked - 160) * hourlyWage * 1.5 : 0;
return (monthlyHoursWorked * hourlyWage) + overtimePay;
}
}
4.SalesEmployee.java
package Message;
class SalesEmployee extends Employee {
private double monthlySales;
private double commissionRate;
public SalesEmployee(String name, int birthMonth, double monthlySales, double commissionRate) {
super(name, birthMonth);
this.monthlySales = monthlySales;
this.commissionRate = commissionRate;
}
public double getSalary(int month) {
double baseSalary = super.getSalary(month);
return monthlySales+(monthlySales * commissionRate);
}
}
5.BasePlusSalesEmployee.java
package Message;
class BasePlusSalesEmployee extends SalesEmployee {
private double baseSalary;
public BasePlusSalesEmployee(String name, int birthMonth, double monthlySales, double commissionRate, double baseSalary) {
super(name, birthMonth, monthlySales, commissionRate);
this.baseSalary = baseSalary;
}
public double getSalary(int month) {
return super.getSalary(month) + baseSalary;
}
}
6.TestEmployee.java
package Message;
public class TestEmployee {
public static void main(String[] args) {
// 创建不同类型的雇员对象并打印工资
Employee employee1 = new SalariedEmployee("李华",
1, 0.0); //假设拿固定工资5000
Employee employee2 = new HourlyEmployee("李明",
2, 15.0, 300); //假设工作了300小时
Employee employee3 = new SalesEmployee("李红",
3, 5000.0, 0.5); //假设销售额为5000,提成率为0.5
Employee employee4 = new BasePlusSalesEmployee("李四",
4, 5000.0, 0.5, 5000.0); //假设销售额为5000,提成率为0.5,底薪为5000
int month = 1; // 假设查询月份的工资
System.out.println("该月份是第1月份");
System.out.println("雇工李华固定工资: " + employee1.getSalary(month));
System.out.println("雇工李明工作了300小时工资: " + employee2.getSalary(month));
System.out.println("雇工李红销售额为5000,提成率为0.5,工资: " + employee3.getSalary(month));
System.out.println("雇工李四销售额为5000,提成率为0.5,底薪为5000,工资: " + employee4.getSalary(month));
}
}
到了这里,关于Java 类对象的综合运用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!