作业
1、定义一个人类(Person),它包含属性:姓名(name),性别(sex);
带两个参数的构造方法; 属性的访问器方法。
定义上面人类的子类学生类(Student), 包括属性:学号(ID),
带参数的构造方法;属性的访问器方法。
package com.neuedu.day06.homework;
public class Person {
private String name;
private char sex;
public Person() {
super();
}
public Person(String name, char sex) {
super();
this.name = name;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
}
package com.neuedu.day06.homework;
public class Student extends Person {
private int ID;
public Student() {
super();
}
public Student(String name, char sex,int ID) {
super(name,sex);
this.ID = ID;
}
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
}
2.1定义抽象的商品类Goods,包含单价unitPrice和数量account二个属性,
计算价格的抽象方法totalPrice();
2.2定义VIP会员价格接口VipPrice,包含discount(折扣率)属性
和reducedPrice()方法(计算折扣后的价格);
2.3定义服装子类Clothing,它继承商品类Goods并实现接口VipPrice,
使VIP会员商品价格享受8折优惠,
并有服装样式style属性、构造方法和显示服装信息的show方法。
package com.neuedu.day06.homework;
public class Clo {
public static void main(String[] args) {
Clothing c=new Clothing(500,10,"连衣裙");
c.show();
}
}
package com.neuedu.day06.homework;
public abstract class Goods {
private double unitPrice;
private int account;
public abstract double totalPrice();
public Goods() {
super();
}
public Goods(double unitPrice, int account) {
super();
this.unitPrice = unitPrice;
this.account = account;
}
public double getUnitPrice() {
return unitPrice;
}
public void setUnitPrice(double unitPrice) {
this.unitPrice = unitPrice;
}
public int getAccount() {
return account;
}
public void setAccount(int account) {
this.account = account;
}
}
package com.neuedu.day06.homework;
public interface VipPrice {
public static double discount = 0.8;
public double reducedPrice();
}
package com.neuedu.day06.homework;
public class Clothing extends Goods implements VipPrice {
private String style;
@Override
public double reducedPrice() {
double reduce = this.totalPrice() * discount;
return reduce;
}
@Override
public double totalPrice() {
double total = super.getAccount() * super.getUnitPrice();
return total;
}
public Clothing() {
super();
}
public Clothing(double unitPrice, int account, String style) {
super(unitPrice, account);
this.style = style;
}
public String getStyle() {
return style;
}
public void setStyle(String style) {
this.style = style;
}
public void show()
{
System.out.println("商品样式:"+this.style);
System.out.println("商品价格:"+this.getUnitPrice());
System.out.println("商品数量"+this.getAccount());
System.out.println("总价格"+reducedPrice());
}
}
3、东软集团的雇员分为以下若干类:
3.1
ColaEmployee :这是所有员工总的父类,属性:员工的姓名,员工的生日月份
方法:getSalary(int month)
根据参数月份来确定工资,如果该月员工过生日,则公司会额外奖励100 元。
3.2 SalariedEmployee : ColaEmployee
的子类,拿固定工资的员工。属性:月薪
3.3 HourlyEmployee :ColaEmployee 的子类,
按小时拿工资的员工,每月工作超出160
小时的部分按照1.5 倍工资发放。属性:每小时的工资、每月工作的小时数
3.4 SalesEmployee :ColaEmployee 的子类,销售人员,工资由月销售额和 提成率决定。属性:月销售额、提成率
3.5
定义一个类Company,在该类中写一个方法,
调用该方法可以打印出某月某个员工的工资数额,(难) 3.6
写一个测试类TestCompany,在main方法,
把若干各种类型的员工放在一个ColaEmployee 数组里,
并打印出数组中每个员工当月的工资。
package com.neuedu.day06.homework;
public class TestCompany {
public static void main(String[] args) {
SalariedEmployee c1 = new SalariedEmployee();
c1.setName("申豪");
c1.setMsal(5000);
c1.setBirthmonth(8);
HourlyEmployee c2 = new HourlyEmployee();
c2.setName("运佳恩");
c2.setHour(170);
c2.setHsal(100);
c2.setBirthmonth(9);
SalesEmployee c3 = new SalesEmployee();
c3.setName("东旭");
c3.setRate(0.75);
c3.setSaleMoney(5000);
c3.setBirthmonth(10);
// 重写是运行时多态,运行时会自动判断这个类时属于哪个子类,自动调用对应重写方法
ColaEmployee c[] = new ColaEmployee[3];
c[0] = c1;
c[1] = c2;
c[2] = c3;
Company com = new Company();
for (int i = 0; i < 3; i++) {
com.print(c[i], 8);
}
}
}
package com.neuedu.day06.homework;
public class ColaEmployee {
private String name;
private int birthmonth;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBirthmonth() {
return birthmonth;
}
public void setBirthmonth(int birthmonth) {
this.birthmonth = birthmonth;
}
//生日补助
public double getSalary(int month) {
//默认是0
int money=0;
//如果传过来的参数月份等于当前员工的生日月份,就返回100,否则返回0
if(month==this.birthmonth) {
money=100;
}
return money;
}
}
package com.neuedu.day06.homework;
/**
* SalariedEmployee : ColaEmployee 的子类,
* 拿固定工资的员工。属性:月薪
*/
public class SalariedEmployee extends ColaEmployee {
private double msal;
public double getSalary(int month)
{
this.msal=this.msal+super.getSalary(month);
return this.msal;
}
public double getMsal() {
return msal;
}
public void setMsal(double msal) {
this.msal = msal;
}
}
package com.neuedu.day06.homework;
/**
* HourlyEmployee :ColaEmployee 的子类,
* 按小时拿工资的员工,每月工作超出160 小时的部分按照1.5
* 倍工资发放。属性:每小时的工资、每月工作的小时数
*/
public class HourlyEmployee extends ColaEmployee {
//工作小时数
private int hour;
//每小时工资
private double hsal;
public double getSalary(int month)
{
double total=0;
if(hour>=160)
{total=160*this.hsal+(this.hour-160)*this.hsal*1.5+super.getSalary(month);}
else {
total=this.hour*hsal+super.getSalary(month);
}
return total;
}
public int getHour() {
return hour;
}
public void setHour(int hour) {
this.hour = hour;
}
public double getHsal() {
return hsal;
}
public void setHsal(double hsal) {
this.hsal = hsal;
}
}
package com.neuedu.day06.homework;
/**
* SalesEmployee :ColaEmployee 的子类,
* 销售人员, 工资由月销售额和提成率决定。
* 属性:月销售额、提成率
*/
public class SalesEmployee extends ColaEmployee {
// 月销售额
private double saleMoney;
// 提成率
private double rate;
public double getSalary(int month)
{
double total=this.saleMoney*this.rate+super.getSalary(month);
return total;
}
public double getSaleMoney() {
return saleMoney;
}
public void setSaleMoney(double saleMoney) {
this.saleMoney = saleMoney;
}
public double getRate() {
return rate;
}
public void setRate(double rate) {
this.rate = rate;
}
}
package com.neuedu.day06.homework;
/**
* 定义一个类Company,在该类中写一个方法,
* 调用该方法可以打印出某月某个员工的工资数额,
*/
public class Company {
public void print(ColaEmployee c, int month) {
System.out.println(c.getName() + "当月的工资" + c.getSalary(month));
}
}