某公司的雇员分为以下若干类:
Employee:
这是所有员工总的父类,
属性:员工的姓名,员工的生日月份。
方法:getSalary(int month) 根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100元。
package com.hp.ycx;
public class Employee {
private String name;//员工姓名
private int 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;
}
public Employee(String name, int month) {
this.name = name;
this.month = month;
}
public Employee() {
}
public int getSalary (int month){
return 0;
}
}
SalariedEmployee:
Employee的子类,拿固定工资的员工。
属性:月薪
package com.hp.ycx;
public class SalariedEmployee extends Employee {
private int yuexin;
public SalariedEmployee(String name,int month,int yuexin){
this.yuexin = yuexin;
}
public int getYuexin() {
return yuexin;
}
public void setYuexin(int yuexin) {
this.yuexin = yuexin;
}
@Override
public int getSalary(int month) {
if (month == getMonth()){
return getYuexin()+100;
}else {
return getYuexin();
}
}
}
HourlyEmployee:
Employee的子类,按小时拿工资的员工,每月工作超出160小时的部分按照1.5倍工资发放。
属性:每小时的工资、每月工作的小时数
package com.hp.ycx;
public class HourlyEmployee extends Employee {
private int xsGongzi;
private int myGongzuoxiaoshi;
public int getXsGongzi() {
return xsGongzi;
}
public void setXsGongzi(int xsGongzi) {
this.xsGongzi = xsGongzi;
}
public int getMyGongzuoxiaoshi() {
return myGongzuoxiaoshi;
}
public void setMyGongzuoxiaoshi(int myGongzuoxiaoshi) {
this.myGongzuoxiaoshi = myGongzuoxiaoshi;
}
public HourlyEmployee(int xsGongzi, int myGongzuoxiaoshi) {
this.xsGongzi = xsGongzi;
this.myGongzuoxiaoshi = myGongzuoxiaoshi;
}
public HourlyEmployee(String name, int month, int xsGongzi, int myGongzuoxiaoshi) {
super(name, month);
this.xsGongzi = xsGongzi;
this.myGongzuoxiaoshi = myGongzuoxiaoshi;
}
public int getSalary(int month) {
if (getMyGongzuoxiaoshi() > 160) {
if (month == getMonth()) {
double v = (getMyGongzuoxiaoshi() - 160) * (1.5 * getXsGongzi());
int i = getXsGongzi() * 160;
return (int) (v + i + 100);
} else {
double v = (getMyGongzuoxiaoshi() - 160) * (1.5 * getXsGongzi());
int i = getXsGongzi() * 160;
return (int) (v + i);
}
}else{
if(month == getMonth()){
int i =(getXsGongzi()*getMyGongzuoxiaoshi())+100;
return i ;
}else{
int i =getXsGongzi()*getMyGongzuoxiaoshi();
return i ;
}
}
}
}
SalesEmployee:
Employee的子类,销售人员,工资由月销售额和提成率决定。
属性:月销售额、提成率
package com.hp.ycx;
public class SalesEmployee extends Employee{
private int yxioshou;
private double ticheng;
public int getYxioshou() {
return yxioshou;
}
public void setYxioshou(int yxioshou) {
this.yxioshou = yxioshou;
}
public double getTicheng() {
return ticheng;
}
public void setTicheng(double ticheng) {
this.ticheng = ticheng;
}
public SalesEmployee(String name, int month, int yxioshou,double ticheng) {
super(name, month);
this.yxioshou = yxioshou;
this.ticheng = ticheng;
}
public SalesEmployee(int yxioshou,double ticheng) {
this.yxioshou = yxioshou;
this.ticheng = ticheng;
}
@Override
public int getSalary(int month) {
if(month == getMonth()){
double v = (getYxioshou() * getTicheng());
int i =(int) v+100;
return i;
}else {
double v = (getYxioshou() * getTicheng());
return (int) v;
}
}
}
BasePlusSalesEmployee:
SalesEmployee的子类,有固定底薪的销售人员,工资由底薪加上销售提成部分。
属性:底薪。
package com.hp.ycx;
public class BasePlusSalesEmployee extends SalesEmployee{
private int dixin;
public BasePlusSalesEmployee(String name, int month, int yxioshou, double ticheng,int dixin) {
super(name, month, yxioshou, ticheng);
this.dixin = dixin;
}
public BasePlusSalesEmployee(int yxioshou, double ticheng,int dixin) {
super(yxioshou, ticheng);
this.dixin = dixin;
}
public int getDixin() {
return dixin;
}
public void setDixin(int dixin) {
this.dixin = dixin;
}
@Override
public int getSalary(int month) {
if (month == getMonth()){
double v = getDixin() + (getYxioshou() * getTicheng());
return (int) (v+100);
}else {
double v = getDixin()+ (getYxioshou() * getTicheng());
return (int) v;
}
}
}
Test 测试类
package com.hp.ycx;
public class Test {
public static void main(String[] args) {
Employee salariedEmployee = new SalariedEmployee("陌然", 8, 4000);
Employee hourlyEmployee = new HourlyEmployee("寂静", 8, 10, 100);
Employee salesEmployee = new SalesEmployee("星夕研", 5, 2000, 3);
Employee basePlusSalesEmployee = new BasePlusSalesEmployee("你好", 5, 1500, 3, 3000);
int s = salariedEmployee.getSalary(8);
System.out.println(salesEmployee.getName()+"月薪是"+s);
int s1 = hourlyEmployee.getSalary(8);
System.out.println(hourlyEmployee.getName()+"月薪是"+s1);
int s2 = salesEmployee.getSalary(5);
System.out.println(salesEmployee.getName()+"月薪是"+s2);
int s3 = basePlusSalesEmployee.getSalary(5);
System.out.println(basePlusSalesEmployee.getName()+"月薪是"+s3);
}
}
运行结果(只有生日月份)
星夕研月薪是4000
寂静月薪是1100
星夕研月薪是6100
你好月薪是7600
Process finished with exit code
设计一个形状类(接口)Shape,方法:求周长和求面积
形状类(接口)的子类(实现类):
Rect(矩形)
Circle(圆形)
Rect类的子类:
Square(正方形)
不同的子类会有不同的计算周长和面积的方法
创建三个不同的形状对象,放在Shape类型的数组里,分别打印出每个对象的周长和面积
Shape
package com.hp.ycx;
public interface Shape {
//求面积
double getArea();
//求周长
double getPerimeter();
}
Circle,Rect
package com.hp.ycx;
public class Circle implements Shape{
private double a = 3.1415926;
private double r;
public double getA() {
return a;
}
public void setA(double a) {
this.a = a;
}
public Circle (double r) {
this.r = r;
}
@Override
public double getArea() {
return a * r * r;
}
@Override
public double getPerimeter() {
return a * r * 2;
}
}
package com.hp.ycx;
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, double height) {
this.width = width;
this.height = height;
}
public Rect(double width) {
this.width = width;
}
@Override
public double getArea() {
return width * height;
}
@Override
public double getPerimeter() {
return (width* height)*2;
}
}
Square
package com.hp.ycx;
public class Square extends Rect{
public Square(double width) {
super(width);
}
public double getArea() {
return getWidth() * getWidth();
}
public double getPerimeter() {
return 4 * getWidth();
}
}
test
package com.hp.ycx;
public class Test {
public static void main(String[] args) {
Shape[] s = new Shape[3];
s[0] = new Circle(3);
System.out.println("---圆的周长和面积---");
System.out.println(s[0].getArea());
System.out.println(s[0].getPerimeter());
s[1] = new Rect(5, 5);
System.out.println("---矩形周长和面积---");
System.out.println(s[1].getArea());
System.out.println(s[1].getPerimeter());
s[2] = new Square(8);
System.out.println("---正方形的周长和面积---");
System.out.println(s[2].getArea());
System.out.println(s[2].getPerimeter());
}
}
结果
---圆的周长和面积---
28.274333400000003
18.849555600000002
---矩形周长和面积---
25.0
50.0
---正方形的周长和面积---
64.0
32.0
Process finished with exit code 0