第15周
阿新 • • 發佈:2021-06-18
題]
• 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 h22; public class ColaEmployee{ public String name; public int month; public double getSalary(int month){ return 0; } public ColaEmployee(String name, int month) { super(); this.name = name; this.month = month; } public ColaEmployee() { super(); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getMonth() { return month; } public void setMonth(int month) { this.month = month; } } package h22; public class SalariedEmployee extends ColaEmployee{ public double yx; public SalariedEmployee(String string, int i, int j){ super(); } public SalariedEmployee(String name, int month, double yuexin, double yx){ this.yx = yx; } public double getSalary(int month) { if (super.month == month) { return yx + 100; } else { return yx; } } } package h22; public class SalesEmployee extends ColaEmployee{ public int yxse; public double tcl; public SalesEmployee() { super(); } public SalesEmployee(String name, int month, int mxiaoshoue, double tichenglv) { super(); this.yxse = mxiaoshoue; this.tcl = tichenglv; } public double getSalary(int month) { if (super.month == month) { return yxse * tcl + 100; } else { return yxse * tcl; } } } package h22; public class HourlyEmployee extends ColaEmployee{ public int gz; public int time; public HourlyEmployee() { super(); } public HourlyEmployee(String name, int month, int gz, int time) { super(); this.gz = gz; this.time = time; } public double getSalary(int month) { if (super.month == month) { if (time > 160) { return gz * 160 + gz * (time - 160) * 1.5 + 100; } else { return gz * time + 100; } } else { if (time > 160) { return gz * 160 + gz * (time - 160) * 1.5; } else { return gz * time; } } } } package h22; public class Company { public void getSalary(ColaEmployee c, int month) { System.out.println(c.name + "在" + month + "月的月薪為" + c.getSalary(month)+"元"); } } package h22; public class TestCompany { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ColaEmployee c1[] = { new SalariedEmployee("xh", 1, 9), new HourlyEmployee("lh", 4, 8, 200), new SalesEmployee("xm", 1, 46, 4) }; for (int i = 0; i < c1.length; i++) { new Company().getSalary(c1[i], 1); } } }
課後作業
• 5、利用介面實現動態的建立物件[選做題]
• 5.1 建立4個類:
• 蘋果
• 香蕉
• 葡萄
• 園丁
• 5.2 在三種水果的構造方法中列印一句話.
• 以蘋果類為例
• class apple
• {
• public apple()
• {
• System.out.println(―建立了一個蘋果類的物件‖);
}
• }
課後作業
• 類圖如下:
• 5.3 要求從控制檯輸入一個字串,根據字串的
值來判斷建立三種水果中哪個類的物件
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; } }
public class textfruit { public static void main(String[] args) { Gardener g = new Gardener(); g.create(); } }