class Car {
// 行驶
public void run() {
System.out.println("行驶");
}
// 刹车
public void braking() {
System.out.println("刹车");
}
}
class Truck extends Car {
public void pullGoods() {
System.out.println("卡车拉货");
}
public void unloadingGoods() {
System.out.println("卸货");
}
// 行驶重写
public void run() {
System.out.println("货车行驶");
}
// 刹车重写
public void braking() {
System.out.println("货车刹车");
}
}
class Bus extends Car {
public void report() {
System.out.println("报站");
}
public void stop() {
System.out.println("停站");
}
// 行驶重写
public void run() {
System.out.println("公共汽车行驶");
}
// 刹车重写
public void braking() {
System.out.println("公共汽车刹车");
}
}
public class TestCar1 {
public static void main(String[] args) {
Bus bus = new Bus();
bus.run();
bus.braking();
}
}
class Car2 {
String engine;//发动机
String wheel;//轮子
public Car2(String engine,String wheel){
this.engine = engine;
this.wheel = wheel;
}
// 行驶
public void run() {
System.out.println("行驶");
}
// 刹车
public void braking() {
System.out.println("刹车");
}
public void show(){
System.out.println(engine+","+wheel);
}
}
class Truck2 extends Car2 {
String warehouse;//货仓
String loadWeight;//载重
public Truck2(String warehouse,String loadWeight,String engine,String wheel){
super(engine,wheel);//显示调用父类带参构造
this.warehouse = warehouse;
this.loadWeight = loadWeight;
}
public void pullGoods() {
System.out.println("卡车拉货");
}
public void unloadingGoods() {
System.out.println("卸货");
}
public void show(){
System.out.println(engine+","+wheel+","+warehouse+","+loadWeight);
}
}
class Bus2 extends Car2 {
String cabin;//客舱
String busload;//载客量
public Bus2(String cabin,String busload,String engine,String wheel){
super(engine,wheel);//显示调用父类带参构造
this.cabin = cabin;
this.busload = busload;
}
public void report() {
System.out.println("报站");
}
public void stop() {
System.out.println("停站");
}
public void show(){
System.out.println(engine+","+wheel+","+cabin+","+busload);
}
}
public class TestCar2 {
public static void main(String[] args) {
Car2 car = new Car2("发动机","轮子");
car.show();
Bus2 bus = new Bus2("客舱","载客量","发动机","轮子");
bus.show();
}
}
任务描述
(1)编写员工类Employee。该类有如下属性:
id(编号int型)
sex(性别 String型)
name(姓名String型)
duty(职务String型)
salary(薪水double型)
holidays(请假天数 int型)
(2)该类有如下方法
display(),无返回值,该方法能打印员工的姓名、性别以及职务三个属性
getDecMoney(int day) 返回值是int型。该方法能计算员工因请假扣除的工资。计算扣除工资的方法是:如果请假天数<=3,则扣款为30×请假天数;如果请假天数超过3天,则扣款为50×请假天数。参数day表示请假天数。(不用考虑是什么原因请假的问题)
(3)该类的构造方法如下
Employee(int id, String sex,String name,String duty, double salary,int holidays)
(4)编写Main类,在Main类的main方法中,创建一个员工类的对象,并调用其display()和getDecMoney方法。
(5)编写Director类(董事),该类继承Employee类,在Employee类的基础上,新添加assistantName(助手姓名)属性。
为Director编写构造方法,以初始化assistantName属性。
(6)董事收入高,责任也大,因此其请假扣款算法与普通员工不同。董事请假扣款算法如下:
如果请假天数<=3,则扣款为50×请假天数;如果请假天数超过3天,则扣款为80×请假天数。
(7)覆盖父类getDecMone(int day)方法。
//选做题:员工工资
class Employee{
protected int id;
protected String sex;
protected String name;
protected String duty;
protected double salary;
protected int holidays;
public void display() {
System.out.println(sex+","+name+","+duty);
}
public Employee() {}
public Employee(int id, String sex, String name, String duty, double salary, int holidays) {
super();
this.id = id;
this.sex = sex;
this.name = name;
this.duty = duty;
this.salary = salary;
this.holidays = holidays;
}
public int getDecMoney(int day) {
if(day > 3) {
salary = salary - day*50;
}else if(day <= 3) {
salary = salary - day*30;
}
return (int)salary;
}
}
class Director extends Employee{
private String assistntName;
public Director(int id, String sex, String name, String duty, double salary, int holidays,String assistntName) {
super(id,sex,name,duty,salary,holidays);
this.assistntName = assistntName;
}
public int getDecMoney(int day) {
if(day > 3) {
salary = salary- day*80;
}else if(day <= 3) {
salary = salary - day*50;
}
return (int)salary;
}
public void display() {
System.out.println(sex+","+name+","+duty+","+assistntName);
}
}
public class TestEmpolyee {
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee emp = new Employee(11,"男","郭靖","会计",5000,5);
emp.display();
int n = emp.getDecMoney(emp.holidays);
System.out.println(n);
Director huangrong = new Director(1, "女", "黄蓉", "懂事", 20000, 5, "穆念慈");
huangrong.display();
int m = huangrong.getDecMoney(huangrong.holidays);
System.out.println(m);
}
}