《Java程序设计》实验6
1、接口应用:接口继承与接口实现
具体要求:
(1)Biology生物接口中定义了breathe()抽象方法;
package homework.test;
public interface Biology {
public void breathe();
}
(2)Animal动物接口继承了Biology接口,增加eat()和sleep()两个抽象方法;
package homework.test;
public interface Animal extends Biology {
public void eat();
public void sleep();
}
(3)Human人类接口继承了Animal接口,增加think()和learn()两个抽象方法;
package homework.test;
public interface Human extends Animal {
public void think();
public void learn();
}
(4)定义一个普通人类Person实现Human接口,并进行测试。
package homework.test;
public class Person implements Human {
@Override
public void eat() {
// TODO Auto-generated method stub
System.out.println("吃");
}
@Override
public void sleep() {
// TODO Auto-generated method stub
System.out.println("睡");
}
@Override
public void breathe() {
// TODO Auto-generated method stub
System.out.println("呼吸");
}
@Override
public void think() {
// TODO Auto-generated method stub
System.out.println("思考");
}
@Override
public void learn() {
// TODO Auto-generated method stub
System.out.println("学习");
}
public static void main(String[] args) {
Person person = new Person();
person.eat();
person.sleep();
person.breathe();
person.think();
person.learn();
}
}
2、动态多态应用:企业员工工资管理
具体要求:某公司的雇员分为以下若干类:
(1)Employee员工类,包含受保护的属性name(姓名)、birthDate(出生日期),public double
getSalary(Employee e)方法(该方法根据传入不同类别的员工,工资计算方法则返回其本月的工资);
package homework.test;
public class Employee {
protected String name;
protected String birthDate;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
System.out.println("姓名为:"+name);
}
public double getSalary(Employee e) {
return 0;
}
public static void main(String[] args) {
Employee a = new SalariedEmployee(2000);
System.out.println("SalariedEmployee:"+a.getSalary(a));
Employee b = new HourlyEmployee(200, 200);
System.out.println("HourlyEmployee:"+b.getSalary(b));
Employee c = new SalesEmployee(200000,0.5);
System.out.println("SalesEmployee:"+c.getSalary(c));
Employee d = new BasePlusSalesEmployee(20000,0.5, 4000);
System.out.println("BasePlusSaleEmployee:"+d.getSalary(d));
}
}
(2)SalariedEmployee类继承Employee类,代表领取固定工资的员工,包含私有属性salary(固定工资);
package homework.test;
public class SalariedEmployee extends Employee {
private double salary;
public SalariedEmployee(double salary) {
this.salary = salary;
}
public double getSalary(Employee e) {
return this.salary;
}
}
(3)HourlyEmployee类继承Employee类,代表按小时拿工资的员工,包含私有属性hourSalary(每小时的工资)、hours(每月工作的小时数)。工资计算方法:月工资=hourSalary * hours,每月工作超出160小时的部分,按照1.5倍小时工资发放。
package homework.test;
public class HourlyEmployee extends Employee {
private double hourSalary;
private double hours;
public HourlyEmployee(double hourSalary,double hours) {
this.hours = hours;
this.hourSalary = hourSalary;
}
public double getSalary(Employee e) {
if(hours > 160) {
return hourSalary * ((hours - 160) * 1.5 + 160);
}
else {
return hours * hourSalary;
}
}
}
(4)SalesEmployee类继承Employee类,代表销售人员,包含受保护的属性sale(月销售额),commissionRate(提成率)。工资计算方法:月工资=sale * commissionRate。
package homework.test;
public class SalesEmployee extends Employee {
protected double sale;
protected double commissionRate;
public SalesEmployee(double sale, double commissionRate) {
super();
this.sale = sale;
this.commissionRate = commissionRate;
}
public double getSalary(Employee e) {
return sale * commissionRate;
}
}
(5)BasePlusSalesEmployee类继承SaleEmployee类,代表有固定底薪的销售人员,工资由底薪加上销售提成部分组成。其私有属性basicSalary(底薪)。工资计算方法:月工资=basicSalary + sale * commissionRate。
package homework.test;
public class BasePlusSalesEmployee extends SalesEmployee {
private double basicSalary;
public BasePlusSalesEmployee(double sale, double commissionRate, double basicSalary) {
super(sale, commissionRate);
this.basicSalary = basicSalary;
}
public double getSalary(Employee e) {
return basicSalary+super.sale*super.commissionRate;
}
}