第十五周作业
Cola公司的雇员分为以下若干类:(知识点:多态) [必做
题]
• 4.1 ColaEmployee :这是所有员工总的父类,属性:员工的
姓名,员工的生日月份。方法:getSalary(int month) 根据参数
月份来确定工资,如果该月员工过生日,则公司会额外奖励
100 元。
• 4.2 SalariedEmployee : ColaEmployee 的子类,拿固定工
资的员工。属性:月薪
课后作业
• 4.3 HourlyEmployee :ColaEmployee 的子类,按小时拿工
资的员工,每月工作超出160 小时的部分按照1.5 倍工资发
放。属性:每小时的工资、每月工作的小时数
• 4.4 SalesEmployee :ColaEmployee 的子类,销售人员,
工资由月销售额和提成率决定。属性:月销售额、提成率
• 4.5 定义一个类Company,在该类中写一个方法,调用该
方法可以打印出某月某个员工的工资数额,写一个测试类
TestCompany,在main方法,把若干各种类型的员工放在一
个ColaEmployee 数组里,并单元出数组中每个员工当月的
工资。
package com.wahaha.Demo;
public class Hepo {
public static void main(String[] args) {
Employee e1 = new SalariedEmployee("张三", 6, 9000);
e1.getSalary(6);
Employee e2 = new HourlyEmployee("李四", 7, 60, 200);
e2.getSalary(6);
Employee e3 = new HourlyEmployee("王五", 2, 60, 150);
e3.getSalary(6);
Employee e4 = new SalesEmployee("赵六", 11, 70000.0, 0.15);
e4.getSalary(6);
Employee e5 = new BasePlusSalesEmployee("孙子", 4, 10000, 0.10, 8000.0);
e5.getSalary(6);
}
}
abstract class Employee {
private String name;
private int birthdayMonth;
public Employee() {
super();
}
public Employee(String name, int birthdayMonth) {
super();
this.name = name;
this.birthdayMonth = birthdayMonth;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBirthdayMonth() {
return birthdayMonth;
}
public void setBirthdayMonth(int birthdayMonth) {
this.birthdayMonth = birthdayMonth;
}
public abstract void getSalary(int month);
}
class SalariedEmployee extends Employee {
private double monthSalary;
public SalariedEmployee() {
super();
}
public SalariedEmployee(String name, int birthdayMonth, double monthSalary) {
super(name, birthdayMonth);
this.monthSalary = monthSalary;
}
public double getMonthSalary() {
return monthSalary;
}
public void setMonthSalary(double monthSalary) {
this.monthSalary = monthSalary;
}
public void getSalary(int month) {
System.out.println("本月工资为:" +(month == getBirthdayMonth() ? (monthSalary + 100) : monthSalary));
}
}
class HourlyEmployee extends Employee {
private double hourlySalary;
private int hours;
public HourlyEmployee() {
super();
}
public HourlyEmployee(String name, int birthdayMonth, double hourlySalary, int hours) {
super(name, birthdayMonth);
this.hourlySalary = hourlySalary;
this.hours = hours;
}
public double getHourlySalary() {
return hourlySalary;
}
public void setHourlySalary(double hourlySalary) {
this.hourlySalary = hourlySalary;
}
public int getHours() {
return hours;
}
public void setHours(int hours) {
this.hours = hours;
}
public void getSalary(int month) {
double salary = 0;
if(hours <= 160) {
salary = hourlySalary * hours;
} else {
salary = ((hours - 160) * 1.5 + 160) * hourlySalary;
}
System.out.println("本月工资为:" +(month == getBirthdayMonth() ? (salary + 100) : salary));
}
}
class SalesEmployee extends Employee {
private double saleroom;
private double rate;
public SalesEmployee() {
super();
}
public SalesEmployee(String name, int birthdayMonth, double saleroom, double rate) {
super(name, birthdayMonth);
this.saleroom = saleroom;
this.rate = rate;
}
public double getSaleroom() {
return saleroom;
}
public void setSaleroom(double saleroom) {
this.saleroom = saleroom;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
public void getSalary(int month) {
System.out.println("本月工资为:" +(month == getBirthdayMonth() ? (saleroom * rate + 100) : saleroom * rate));
}
}
class BasePlusSalesEmployee extends SalesEmployee {
private double basicSalary;
public BasePlusSalesEmployee() {
super();
}
public BasePlusSalesEmployee(String name, int birthdayMonth, double saleroom, double rate, double basicSalary) {
super(name, birthdayMonth, saleroom, rate);
this.basicSalary = basicSalary;
}
public double getBasicSalary() {
return basicSalary;
}
public void setBasicSalary(double basicSalary) {
this.basicSalary = basicSalary;
}
public void getSalary(int month) {
double salary = getSaleroom() * getRate() + basicSalary;
System.out.println("本月工资为:" +(month == getBirthdayMonth() ? (salary + 100) : salary));
}
}
课后作业
• 5、利用接口实现动态的创建对象[选做题]
• 5.1 创建4个类:
• 苹果
• 香蕉
• 葡萄
• 园丁
• 5.2 在三种水果的构造方法中打印一句话.
• 以苹果类为例
• class apple
• {
• public apple()
• {
• System.out.println(―创建了一个苹果类的对象‖);
}
• }
课后作业
• 类图如下:
• 5.3 要求从控制台输入一个字符串,根据字符串的
值来判断创建三种水果中哪个类的对象
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
package Week15;import java.util.Scanner;public interface Fruit {}class Apple implements Fruit { public Apple() { System.out.println("创建了一个苹果对象"); }}class Banana implements Fruit { public Banana() { System.out.println("创建了一个香蕉对象"); }}class Putao implements Fruit { public Putao() { System.out.println("创建了一个葡萄对象"); }}class Gardener { public Fruit create() { Fruit f = null; Scanner input = new Scanner(System.in); String name = input.next(); if (name.equals("苹果")) { f = new Apple(); } else if (name.equals("香蕉")) { f = new Banana(); } else if (name.equals("葡萄")) { f = new Putao(); } else { System.out.println("不会种"); } return f; }} |
|
1
2
3
4
5
6
7
|
public class textfruit { public static void main(String[] args) { Gardener g = new Gardener(); g.create(); }} |


浙公网安备 33010602011771号