java十五周作业
1.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 java15;
public class ColaEmployee {
protected String name;
protected int months;
protected int birthday;
public ColaEmployee(String name, int months, int birthday) {
super();
this.name = name;
this.months = months;
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMonths() {
return months;
}
public void setMonths(int months) {
this.months = months;
}
public int getBirthday() {
return birthday;
}
public void setBirthday(int birthday) {
this.birthday = birthday;
}
public void getSalary(int month){
}
}
package java15;
public class SalariedEmployee extends ColaEmployee {
private double money;
public SalariedEmployee(String name, int months, int birthday, double money) {
super(name, months, birthday);
if(super.getMonths()==super.getBirthday()){
System.out.println("员工"+name+months+"月的工资为"+(money+100));
}else{
System.out.println("员工"+name+months+"月的工资为"+money);
}
}
}
package java15;
public class HourlyEmployee extends ColaEmployee{
private double hours;
private int mon;
public HourlyEmployee(String name, int months, int birthday, double hours,
int mon) {
super(name, months, birthday);
this.hours=hours;
this.mon=mon;
if(super.getBirthday()!=super.getMonths()&&super.getMonths()>=160){
System.out.println("员工"+name+months+"月的工资为"+(mon*160+(hours-160)*mon*1.5));
}else if(super.getBirthday()==super.getMonths()&&super.getMonths()>=160){
System.out.println("员工"+name+months+"月的工资为"+(mon*160+(hours-160)*mon*1.5+100));
}else if(super.getBirthday()==super.getMonths()&&super.getMonths()<160) {
System.out.println("员工"+name+months+"月的工资为"+(mon*hours+100));
}else if(super.getBirthday()!=super.getMonths()&&super.getMonths()<160) {
System.out.println("员工"+name+months+"月的工资为"+mon*hours);
}
}
}
package java15;
public class SalesEmployee extends ColaEmployee {
private double x;
private double l;
public SalesEmployee(String name, int months, int birthday,double x,double l) {
super(name, months, birthday);
if(super.getMonths()==super.getBirthday()){
System.out.println("员工"+super.getName()+"工资为"+x*l+100);
}else{
System.out.println("员工"+super.getName()+"工资为"+x*l);
}
}
}
package java15;
public class TestCompany {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ColaEmployee[]y=new ColaEmployee[6];
y[0]=new SalariedEmployee("A", 2, 2, 2000);
y[1]=new HourlyEmployee("B", 4, 4, 170, 13);
y[2]=new SalariedEmployee("C", 6, 6, 2000);
y[3]=new SalariedEmployee("D", 3, 3, 2000);
y[4]=new SalesEmployee("E", 7, 7, 20000, 0.13);
y[5]=new SalesEmployee("F",8, 8, 20000, 0.13);
}
}

5、利用接口实现动态的创建对象[选做题]
• 5.1 创建4个类:
• 苹果
• 香蕉
• 葡萄
• 园丁
• 5.2 在三种水果的构造方法中打印一句话.
• 以苹果类为例
• class apple
• {
• public apple()
• {
• System.out.println(―创建了一个苹果类的对象‖);
}
• }
课后作业
• 类图如下:
• 5.3 要求从控制台输入一个字符串,根据字符串的
值来判断创建三种水果中哪个类的对象
package java15;
import java.util.Scanner;
public class Gardener {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Gardener g=new Gardener();
g.creat();
}
public Fruit creat(){
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;
}
}
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("创建了一个葡萄对象");
}
}


浙公网安备 33010602011771号