文章目录
- 考察抽象方法和继承
- 餐厅
- 牛排
- 汉堡
- 测试类
- 接口的考察以及对于接口抽象方法的重写
- 用户接口
- 工人
- 经理
- 管理者
- 工厂类(也就是测试类)
- 主要是对于接口和抽象类的综合练习
- 保养接口
- 交通工具抽象类
- 具体交通工具类(他实现了抽象类和接口中的所有抽象方法)
- GPS接口
- 接受坐标设备类
- 任务类
- 测试类
考察抽象方法和继承
子类可以直接使用父类中的可以访问的成员方法(也就是public、protected修饰的成员方法,抽象方法子类必须重写。)
设计一个模板模式,实现餐厅(restaurant)吃饭的过程:点单(order)、吃东西(repast)、买单(pay),其中吃东西可以是牛排、披萨、汉堡等(可自己定义),类图图1所示。编写测试类,测试模板方法。
餐厅
//先写父类的抽象方法
//首先要知道那些方法是可以在父类确定的
//哪些是需要自类重写的
public abstract class Restaurant {
//子类可以调用父类的public成员方法
public void order() {
System.out.println("点单");
}
//抽象方法需要带abstract
//之后可以子类可以重写
public abstract void repast();
public void pay() {
System.out.println("买单");
}
public void process() {
order();
repast();
pay();
}
}
牛排
public class Beafsteak extends Restaurant{
//此处一定要重写父类抽象方法不然会报错
@Override
public void repast() {
System.out.println("吃牛排");
}
}
汉堡
public class Humburger extends Restaurant{
@Override
public void repast() {
System.out.println("吃汉堡");
}
}
测试类
public class Main {
public static void main(String[] args) {
Beafsteak beafsteak = new Beafsteak();
Humburger humburger = new Humburger();
beafsteak.process();
humburger.process();
}
}
接口的考察以及对于接口抽象方法的重写
接口中都是抽象方法和常量 ,在接口内部不能改变的
利用简单工厂模式,实现某OA系统中的权限管理。
1.角色划分
· 用户分为员工(Employee)、经理(Manager)和管理员(Administrator)
2.权限划分
· 员工权限:创建请假条
· 经理权限:创建和审批请假条
· 管理员权限:创建和管理请假条
用户接口
public interface User {
public void LeaveOperation();
}
工人
public class Employee implements User {
@Override
public void LeaveOperation() {
System.out.println("创建请假条");
}
}
经理
public class Manager implements User {
@Override
public void LeaveOperation() {
System.out.println("创建和审批请假条");
}
}
管理者
public class Administrator implements User {
@Override
public void LeaveOperation() {
System.out.println("创建和管理请假条");
}
}
工厂类(也就是测试类)
public class UserFactory {
public static void main(String[] args) {
//使用向上转型或者直接使用类名创建对应的引用
// User user1 = new Employee();
// User user2 = new Manager();
// User user3 = new Administrator();
Employee user1 = new Employee();
Manager user2 = new Manager();
Administrator user3 = new Administrator();
user1.LeaveOperation();
user2.LeaveOperation();
user3.LeaveOperation();
}
}
主要是对于接口和抽象类的综合练习
编写一个模拟物流快递系统的程序,模拟物流公司后台系统处理货物的过程。系统设计如下图所示:
保养接口
public interface Careable {
public void upkeep();
}
交通工具抽象类
public abstract class Transportation implements Careable{
public String number;
public String model;
public String admin;
public Transportation(String number, String model, String admin) {
this.number = number;
this.model = model;
this.admin = admin;
}
public abstract void transport();
}
具体交通工具类(他实现了抽象类和接口中的所有抽象方法)
public class ExpressTransprotation extends Transportation {
public ExpressTransprotation(String number, String model, String admin) {
super(number, model, admin);
}
@Override
public void upkeep() {
System.out.println("货物运输完成");
System.out.println(admin + "已经将编号为" + number + "的" + model + "归还");
System.out.println("运输车辆保养完毕");
}
@Override
public void transport() {
System.out.println(admin + "正驾驶编号为" + number + "的" + model);
System.out.println("货物运输中");
}
}
GPS接口
public interface GPS {
public void showCoordinate();
}
接受坐标设备类
public class Phone implements GPS{
private String coordinate;
public Phone(String coordinate) {
this.coordinate = coordinate;
}
@Override
public void showCoordinate() {
System.out.println("货物当前坐标为:" + coordinate);
}
}
任务类
import com.sun.media.jfxmediaimpl.HostUtils;
import com.sun.org.apache.xpath.internal.objects.XNumber;
public class SendTask {
//快递单号
private String number;
//重量
private double goodWeight;
//构造方法
public SendTask(String number, double goodWeight) {
this.number = number;
this.goodWeight = goodWeight;
}
//运货前处理
public void sendBefore() {
System.out.println("订单开始处理,仓库验货中。。。");
System.out.println("货物重量" + goodWeight);
System.out.println("货物校验完毕!");
System.out.println("货物填装完毕");
System.out.println("已通知运货人");
System.out.println("快递单号" + number);
}
//运货前处理
public void send(Transportation t, GPS too) {
t.transport();
too.showCoordinate();
}
//运货后处理
public void sendAfter(Transportation t) {
t.upkeep();
}
}
测试类
public class Main {
public static void main(String[] args) {
//使用向上转型(多态)借用父类访问子类成员
Transportation e = new ExpressTransprotation("陕A12345","IVICO","张");
GPS gps = new Phone("1231,121");
//任务对象
SendTask s = new SendTask("EMS123456",23.3);
s.sendBefore();
System.out.println("=================");
s.send(e,gps);
System.out.println("=================");
s.sendAfter(e);
}
}