十五周作业
4、 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 Test4; public class ColaEmployee { String name; int month; public ColaEmployee(String name, int month) { super(); this.name = name; this.month = month; } public ColaEmployee() { // TODO 自动生成的构造函数存根 } public int getSalary(int month) { if(this.month==month){ return 100; }else{ return 0; } } public void show() { } }
package Test4; public class SalariedEmployee extends ColaEmployee { int salary; public SalariedEmployee() { // TODO 自动生成的构造函数存根 } public SalariedEmployee(String name, int month, int salary) { super(name, month); this.salary = salary; } public void show() { System.out.println(name+"的生日是"+month+"月,一个月工资是"+salary); } }
package Test4; public class HourlyEmployee extends ColaEmployee { int hoursalary; int workhour; public HourlyEmployee() { // TODO 自动生成的构造函数存根 } public HourlyEmployee(String name, int month, int hoursalary, int workhour) { super(name, month); this.hoursalary = hoursalary; this.workhour = workhour; } public void show() { System.out.println(name+"的生日是"+month+"月,每小时工资"+hoursalary+",工作了"+workhour+"小时"); } }
package Test4; public class SalesEmployee extends ColaEmployee { int monthsell; double commission; public SalesEmployee() { // TODO 自动生成的构造函数存根 } public SalesEmployee(String name, int month, int monthsell, double commission) { super(name, month); this.monthsell = monthsell; this.commission = commission; } public void show() { System.out.println(name+"的生日是"+month+"月,月销售额是"+monthsell+",提成是"+commission); } }
package Test4; public class Company { public void money(SalariedEmployee s,int salary,int month) { System.out.println("他在"+month+"月获得的工资是"+(s.getSalary(month)+salary)); } public void money(HourlyEmployee h,int hoursalary,int workhour,int month) { double money=0; if(workhour>160) { money=(h.hoursalary*160+(h.workhour-160)*h.hoursalary*1.5)+h.getSalary(month); }else { money=(h.hoursalary*160+h.getSalary(month)); } System.out.println("他在"+month+"月获得的工资是"+money); } public void money(SalesEmployee f,int monthsell, double commission,int month) { System.out.println("他在"+month+"月获得的工资是"+(monthsell*(1+commission)+f.getSalary(month))); } }
package Test4; public class TestCompany { public static void main(String[] args) { SalariedEmployee s=new SalariedEmployee("张三",3,2000); s.show(); Company s2=new Company(); s2.money(s, 2000,3); HourlyEmployee h=new HourlyEmployee("李四",5,200,180); h.show(); Company h2=new Company(); h2.money(h, 200, 180, 3); SalesEmployee f=new SalesEmployee("王二",7,8000,0.4); f.show(); Company f2=new Company(); f2.money(f, 8000, 0.4, 7); } }

5、利用接口实现动态的创建对象[选做题]
• 5.1 创建4个类: • 苹果 • 香蕉 • 葡萄 • 园丁
• 5.2 在三种水果的构造方法中打印一句话.
• 以苹果类为例
• class apple
• {
• public apple()
• {
• System.out.println(―创建了一个苹果类的对象‖); }
• }
• 5.3 要求从控制台输入一个字符串,根据字符串的 值来判断创建三种水果中哪个类的对象




package Test5; public interface Fruit { public void fruit(); } ======================= package Test5; public class Apple implements Fruit{ public void fruit(){ System.out.println("创建了一个苹果类的对象"); } } ======================== package Test5; public class Banana implements Fruit{ public void fruit(){ System.out.println("创建了一个香蕉类的对象"); } } ========================= package Test5; public class Grape implements Fruit{ public void fruit(){ System.out.println("创建了一个葡萄类的对象"); } } ========================= package Test5; import java.util.Scanner; public class Gardener { public static void creat() { Scanner input= new Scanner(System.in); String name=input.next(); if(name.equals("苹果")) { Apple a=new Apple(); a.fruit(); }else if(name.equals("香蕉")) { Banana b=new Banana(); b.fruit(); }else if(name.equals("葡萄")) { Grape g=new Grape(); g.fruit(); }else { System.out.println("不会种!"); } } public static void main(String[] args) { System.out.println("输入要创建的对象"); Gardener.creat(); } }

浙公网安备 33010602011771号