JAVA 快餐 8 方法的重写与重载/多态方法处理
一、设计时多态的方法
package com.example.hibernate.pro3; public class DeviceService { // // 根据接收的参数执行不同的行为 // public String getDevice(){ return "医师领取的设备"; } public String getDevice(String notes){ return "医护领取的设备"; } public String getDevice(String offices,String notes){ return "病人量取设备"; } ///////////////////f public static void main(String[] args) { //创建设备服务对象 DeviceService ds = new DeviceService(); //领取设备 String res = ds.getDevice();//医师领取的设备 String res2 = ds.getDevice("手术用品");//医护领取的设备 String res3 = ds.getDevice("骨科","固定带");//病人量取设备 } }
二、运行时的多态
package com.example.hibernate.pro4; import com.example.hibernate.pro3.DeviceService; /** * Hospital * @Remark:医院的类型 */ public class Hospital { /** * @param patient 病人 * @param dept 科室 */ public void register(Patient patient, Department dept) { System.out.println("开始挂号到对应的科室:" + dept.name); dept.treatment(patient); } public static void main(String[] args) { //创建医院对象 Hospital hs = new Hospital(); //骨科 Orthopaedics op = new Orthopaedics(); //外科 Surgery sg = new Surgery(); sg.name = "外科"; //病人 Patient patient = new Patient(); patient.name = "TMP"; //挂号 hs.register(patient, sg); } } /** * 病人 */ class Patient{ public int id; public String name; public String gender; public int age; public float health; } /** * 科室 */ class Department{ public int id; public String name; public String intro; //接收病人的方法 public void treatment(Patient patient){ System.out.println(this.name + "接收病人,开始治疗"); } } class Orthopaedics extends Department{ public void treatment(Patient patient){ System.out.println(this.name + "骨科接收到病人,开始治疗"); } } class Surgery extends Department{ public void treatment(Patient patient){ System.out.println(this.name + "外科接收到病人,开始治疗"); } }