1、理解练习并能熟练完成今日课堂案例
2、某公司的雇员分为以下若干类:
Employee:
这是所有员工总的父类,
属性:员工的姓名,员工的生日月份。
方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100元。
SalariedEmployee:
Employee的子类,拿固定工资的员工。
属性:月薪
HourlyEmployee:
Employee的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5倍工资发放。
属性:每小时的工资、每月工作的小时数
SalesEmployee:
Employee的子类,销售人员,工资由月销售额和提成率决定。
属性:月销售额、提成率
BasePlusSalesEmployee:
SalesEmployee的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。
属性:底薪。
写一个程序,把若干各种类型的员工放在一个Employee数组里,写一个方法,打印出某月每个员工的工资数额。
注意:要求把每个类都封装,不允许非私有化属性。
不同的子类会有不同的计算周长和面积的方法
创建三个不同的形状对象,放在Shape类型的数组里,分别打印出每个对象的周长和面积
package com.hp.work;
import java.util.Calendar;
public class Employee {
private String name;
private int month;//出生月份
//如这个月过生日,奖励100元
public static double getSalary(int month){
//获取当前月份
Calendar ca= Calendar.getInstance();
int m= ca.get(Calendar.MONTH)+1;
if(month==m){
return 100;
}else {
return 0;
}
}
public Employee() {
}
public Employee(String name, int month) {
this.name = name;
this.month = month;
}
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 com.hp.work;
public class HourlyEmployee extends Employee{
private double HourlyWage;//每小时工资
private double HoursWorked;//每月工作的小时数
public double gethour(int HoursWorked,double HourlyWage,int month){
double d=0;
if(HoursWorked>160){
d=160*HourlyWage+(HoursWorked-160)*HourlyWage*1.5+Employee.getSalary(month);
}else {
d=HoursWorked*HourlyWage+Employee.getSalary(month);
}
return d;
}
public double getHourlyWage() {
return HourlyWage;
}
public void setHourlyWage(double hourlyWage) {
HourlyWage = hourlyWage;
}
public double getHoursWorked() {
return HoursWorked;
}
public void setHoursWorked(double hoursWorked) {
HoursWorked = hoursWorked;
}
public HourlyEmployee() {
}
public HourlyEmployee(double hourlyWage, double hoursWorked) {
HourlyWage = hourlyWage;
HoursWorked = hoursWorked;
}
public HourlyEmployee(String name, int birthday, double hourlyWage, double hoursWorked) {
super(name, birthday);
HourlyWage = hourlyWage;
HoursWorked = hoursWorked;
}
}
package com.hp.work;
//提成
public class SalesEmployee extends Employee{
private double sales;//月销售额
private double rate;//提成率
public double getroyalty(double sales,double rate){
return sales*rate;
}
}
package com.hp.work;
public class BasePlusSalesEmployee extends SalesEmployee{
private double salary;//底薪
public double getroyalty(int month){
return salary+super.getSalary(month);
}
}
package com.hp.work;
//拿固定工资的员工
public class SalariedEmployee extends Employee{
private double monthly; //月薪
public double getmonth(double monthly,int birthday){
//固定工资的员工
double d= monthly+Employee.getSalary(birthday);
return d;
}
public SalariedEmployee(double monthly) {
this.monthly = monthly;
}
public SalariedEmployee() {
}
public double getMonthly() {
return monthly;
}
public void setMonthly(double monthly) {
this.monthly = monthly;
}
}
package com.hp.work;
public class HourlyEmployee extends Employee{
private double HourlyWage;//每小时工资
private double HoursWorked;//每月工作的小时数
public double gethour(int HoursWorked,double HourlyWage,int month){
double d=0;
if(HoursWorked>160){
d=160*HourlyWage+(HoursWorked-160)*HourlyWage*1.5+Employee.getSalary(month);
}else {
d=HoursWorked*HourlyWage+Employee.getSalary(month);
}
return d;
}
public double getHourlyWage() {
return HourlyWage;
}
public void setHourlyWage(double hourlyWage) {
HourlyWage = hourlyWage;
}
public double getHoursWorked() {
return HoursWorked;
}
public void setHoursWorked(double hoursWorked) {
HoursWorked = hoursWorked;
}
public HourlyEmployee() {
}
public HourlyEmployee(double hourlyWage, double hoursWorked) {
HourlyWage = hourlyWage;
HoursWorked = hoursWorked;
}
public HourlyEmployee(String name, int birthday, double hourlyWage, double hoursWorked) {
super(name, birthday);
HourlyWage = hourlyWage;
HoursWorked = hoursWorked;
}
}
package com.hp.work;
public class Test {
public static void main(String[] args) {
SalariedEmployee s1 = new SalariedEmployee();
s1.setName("张三");
System.out.println(s1.getName()+"月薪:"+s1.getmonth(10000, s1.getMonth()));
HourlyEmployee h1 = new HourlyEmployee();
h1.setName("李四");
System.out.println(h1.getName()+"时薪:"+h1.gethour(150, 10, h1.getMonth()));
BasePlusSalesEmployee b1=new BasePlusSalesEmployee();
b1.setName("王五");
}
}
3、设计一个形状类(接口)Shape,方法:求周长和求面积
形状类(接口)的子类(实现类):
Rect(矩形)
Circle(圆形)
Rect类的子类:
Square(正方形)
package com.hp.c;
public class Circle {
private double r;//圆的半径
private double p;//圆周率
//周长
public double cc(double r,double p){
double cc=2*p*r;
System.out.println("圆的周长:");
return cc;
}
//面积
public double ss(double r,double p){
double ss=p*r*r;
System.out.println("圆的面积:");
return ss;
}
public Circle() {
}
public Circle(double r) {
this.r = r;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
}
package com.hp.c;
import java.util.Scanner;
public class Rect {
private double longer;//长
private double width;//宽
//周长
public double c(double longer,double width){
double c=(longer+width)*2;
System.out.println("矩形的周长:");
return c;
}
//面积
public double s(double longer,double width){
double s=longer*width;
System.out.println("矩形的面积:");
return s;
}
public Rect() {
}
public Rect(double longer, double width) {
this.longer = longer;
this.width = width;
}
public double getLonger() {
return longer;
}
public void setLonger(double longer) {
this.longer = longer;
}
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
}
package com.hp.c;
public class Test {
public static void main(String[] args) {
Rect r1 = new Rect();
System.out.println(r1.c(2, 3));
System.out.println(r1.s(2, 3));
Circle c1=new Circle();
System.out.println(c1.cc(2, 3.14));
System.out.println(c1.ss(2, 3.14));
}
}