一、
1.父类Employee
package com.hp.demo1;
//Employee类
public class Employee {
private String name; //员工姓名
private int month; //生日
private double salary; //工资
//构造器
public Employee() {
}
public Employee(String name, int month, double salary) {
this.name = name;
this.month = month;
this.salary = salary;
}
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;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
public double getSalary(int month){
//判断是否生日,如果生日额外奖励100元
if (this.month == month) {
salary += 100;
}
System.out.println("该员工"+getName()+"的工资薪水为:"+salary);
return salary;
}
}
2.子类HourlyEmployee
package com.hp.demo1;
//HourlyEmployee类
//继承父类Employee
public class HourlyEmployee extends Employee{
private int hour; //小时
private double price; //代价
private double salary02; //工资
//构造器
public HourlyEmployee() {
}
public HourlyEmployee(int hour, double price, double salary02) {
this.hour = hour;
this.price = price;
this.salary02 = salary02;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public double getSalary02() {
return salary02;
}
public void setSalary02(double salary02) {
this.salary02 = salary02;
}
@Override
public double getSalary(int month) {
//判断工作时间是否超过160小时,超出的部分按照1.5的工资计算
if (hour <= 160) {
salary02 = price * hour;
}else {
hour = hour - 160;
salary02 = 160 * price + hour * 1.5 * price;
}
//判断是否生日,如果生日额外奖励100元
if (getMonth() == month) {
salary02 += 100;
}
System.out.println("该员工"+getName()+"的工资薪水为:"+salary02);
return salary02;
}
}
3.子类SalariedEmployee
package com.hp.demo1;
//SalariedEmployee类
//继承父类Employee
public class SalariedEmployee extends Employee{
private double salary01; //工资
//构造器
public SalariedEmployee() {
}
public SalariedEmployee(double salary01) {
this.salary01 = salary01;
}
public double getSalary01() {
return salary01;
}
public void setSalary01(double salary01) {
this.salary01 = salary01;
}
public double getSalary(int month){
//判断是否生日,如果生日额外奖励100元
if (this.getMonth() == month) {
salary01 += 100;
}
System.out.println("该员工"+getName()+"的工资薪水为:"+salary01);
return salary01;
}
}
4.子类SalesEmployee
package com.hp.demo1;
//SalesEmployee类
//继承父类Employee
public class SalesEmployee extends Employee{
private double salemonth; //销售额
private double rate; //提成率
//构造器
public SalesEmployee() {
}
public SalesEmployee(double salemonth, double rate) {
this.salemonth = salemonth;
this.rate = rate;
}
public double getSalemonth() {
return salemonth;
}
public void setSalemonth(double salemonth) {
this.salemonth = salemonth;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
@Override
public double getSalary(int month) {
//判断是否生日,如果生日额外奖励100元
if (this.getMonth() == month) {
//如果生日,工资=月销售额*提成率+奖励100
System.out.println("该员工"+getName()+"的工资薪水为:"+ (100 + salemonth * rate));
return (salemonth * rate) + 100;
}else {
//如果没有生日,工资=月销售额*提成率
System.out.println("该员工"+getName()+"的工资薪水为:"+ salemonth * rate);
return salemonth * rate;
}
}
}
5.继承了 4 的子类
package com.hp.demo1;
//BasePlusSalesEmployee类
//继承父类SalesEmployee
public class BasePlusSalesEmployee extends SalesEmployee{
private double minsalary; //底薪
private double salemonth01; //销售额
private double rate01; //提成率
//构造器
public BasePlusSalesEmployee() {
}
public BasePlusSalesEmployee(double minsalary, double salemonth01, double rate01) {
this.minsalary = minsalary;
this.salemonth01 = salemonth01;
this.rate01 = rate01;
}
public double getMinsalary() {
return minsalary;
}
public void setMinsalary(double minsalary) {
this.minsalary = minsalary;
}
public double getSalemonth01() {
return salemonth01;
}
public void setSalemonth01(double salemonth01) {
this.salemonth01 = salemonth01;
}
public double getRate01() {
return rate01;
}
public void setRate01(double rate01) {
this.rate01 = rate01;
}
@Override
public double getSalary(int month) {
//判断是否生日,如果生日额外奖励100元
if (this.getMonth() == month) {
//如果生日,工资=底薪+月销售额*提成率+奖励100
System.out.println("该员工"+getName()+"的工资薪水为:"+ (minsalary + salemonth01 * rate01 + 100));
return minsalary + salemonth01 * rate01 + 100;
}else {
//如果没有生日,工资=底薪+月销售额*提成率
System.out.println("该员工"+getName()+"的工资薪水为:"+ (minsalary + salemonth01 * rate01));
return minsalary + salemonth01 * rate01;
}
}
}
6.测试类
package com.hp.demo1;
//测试类
//某公司
public class Test {
public static void main(String[] args) {
Employee e1 = new Employee("可莉" , 7 , 900);
e1.getSalary(7);
SalariedEmployee s1 = new SalariedEmployee();
s1.setName("影");
s1.setSalary01(800);
s1.setMonth(1);
s1.getMonth();
s1.getSalary(5);
HourlyEmployee h1 = new HourlyEmployee(190 , 36 , 0);
h1.setName("荧");
h1.setMonth(5);
h1.getMonth();
h1.getSalary(10);
SalesEmployee s2 = new SalesEmployee(280 , 0.8);
s2.setName("神子");
s2.setMonth(7);
s2.getMonth();
s2.getSalary(7);
BasePlusSalesEmployee b1 = new BasePlusSalesEmployee(300 , 800 , 0.3);
b1.setName("琴");
b1.setMonth(9);
b1.getMonth();
b1.getSalary(10);
}
}
7.运行结果
二、
1.矩形
package com.hp.demo2;
//设计一个形状类(接口)Shape,方法:求周长和求面积
//形状类
//矩形类
public class Rect implements Shape{
private double width; //宽度
private double height; //高
public double getWidth() {
return width;
}
public void setWidth(double width) {
this.width = width;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
//一个有参构造,将用于子类正方形
public Rect(double width){
this.width = width;
}
//有参构造
public Rect(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double getArea() {
//面积
return width * height;
}
@Override
public double getPerimeter() {
//周长
return (width + height) * 2;
}
}
2.圆形
package com.hp.demo2;
//设计一个形状类(接口)Shape,方法:求周长和求面积
//形状类
//圆形类
public class Circle implements Shape{
private final double PI = 3.14; //圆周率
private double r; //半径
public Circle(double r) {
this.r = r;
}
public double getPI() {
return PI;
}
public double getR() {
return r;
}
public void setR(double r) {
this.r = r;
}
@Override
public double getArea() {
//面积
return PI * r * r;
}
@Override
public double getPerimeter() {
//周长
return PI * r * 2;
}
}
3.正方形
package com.hp.demo2;
//设计一个形状类(接口)Shape,方法:求周长和求面积
//形状类
//正方形类
//继承父类矩形Rect
public class Square extends Rect{
public Square(double width) {
super(width);
}
@Override
public double getArea() {
//面积
return getWidth() * getWidth();
}
@Override
public double getPerimeter() {
//周长
return getWidth() * 4;
}
}
4.接口
package com.hp.demo2;
//接口
public interface Shape {
//计算面积的方法
double getArea();
//计算周长的方法
double getPerimeter();
}
5.测试类
package com.hp.demo2;
//测试类
public class Test {
public static void main(String[] args) {
Shape[] shape = new Shape[3];
shape[0] = new Circle(5);
System.out.println("圆形周长和面积");
System.out.println(String.format("%.2f", shape[0].getArea()));
System.out.println(shape[0].getPerimeter());
shape[1] = new Rect(4, 6);
System.out.println("矩形周长和面积");
System.out.println(shape[1].getArea());
System.out.println(shape[1].getPerimeter());
shape[2] = new Square(6);
System.out.println("正方形周长和面积-");
System.out.println(shape[2].getArea());
System.out.println(shape[2].getPerimeter());
}
}
6.运行结果