Java基础练习5

考察抽象方法和继承

子类可以直接使用父类中的可以访问的成员方法(也就是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);
    }
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值