JS面向对象编程实例--医院医生病人模型简单案例 面向对象的学习应用

这篇博客通过一个医院医生病人模型的实例,探讨了JavaScript中的面向对象编程应用。作者作为初学者,分享了对面向对象的理解,并在代码中添加了详细注释以便读者理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.关于简单的OOP开发案例

2.主要在于面向对象的理解与应用

3.铁子们,笔者是个小白,有些地方写的不好见谅见谅

4.文档中我都给了注释也是方便大家一眼就看明白

//xx医院
class Xxhospital {
    constructor(Hname, deptList) {
        this.Hname = Hname;
        this.deptList = deptList;
    }

    /**打印所有的部门以及部门成员 */
    showDeptAndDoct() {
        // // console.log(this.deptList);
        // // console.log(this.deptList[0].doctorList);
        // // console.log(this.deptList[1].doctorList);
        // console.log(`----------${this.Hname} 成员列表----------`);
        // for (const iterator of this.deptList) {
        //     console.log(`--------${iterator.deptname}科室医生列表---------`);
        //     for (let i = 0; i < iterator.doctorList.length; i++) {
        //         console.log(iterator.doctorList[i].doctorname);
        //     }
        //     console.log(`--------${iterator.deptname}科室医生列表 END---------`);
        // }
        // // console.log(this.deptList[0].deptname);
        // // console.log(this.deptList[0].doctorList[0].doctorname);
        // // console.log(this.deptList[1].deptname);
        for (const iterator of this.deptList) {
            iterator.showDoct();
        }
        console.log(`----------成员列表 END----------`);
    }
}
//科室
class Department {
    constructor(deptname, doctorList) {
        this.deptname = deptname;
        this.doctorList = doctorList;
    }

    /**打印医生列表 */
    showDoct() {
        console.log(`---------${this.deptname}的医生名单----------`);
        for (const iterator of this.doctorList) {
            console.log(iterator.doctorname);
        }
        console.log(`---------${this.deptname}的医生名单 END----------`);
    }
}
//医生
class Doctor {
    medicalhistoryList = new Array(); //病历列表
    prescriptionList = new Array(); //处方列表
    patientList = new Array(); //病人列表
    constructor(doctorname, dept, patient) {
        this.doctorname = doctorname;
        this.dept = dept;
        this.patient = patient;
    }

    /**打印医生具体信息 */
    show() {
        console.log(this.doctorname);
        console.log(this.dept.deptname);
    }

    /**医生属于那个部门 */
    belongDept() {
        console.log(`医生属于:${this.dept.deptname}`);
    }

    /**被预约 */
    beRegistration(p) {
        console.log(`预约成功!该医生的信息如下`);
        this.patientList.push(p); //将病人存入到patientList列表中
        this.show(); //调用show方法打印医生具体信息
    }

    /**查看病人的信息 */
    showRePatients() {
        if (this.patientList.length == 0) console.log('还没有病人预约您');
        else {
            console.log(`-----${this.doctorname}的预约列表`);
            for (const iterator of this.patientList) {
                console.log(iterator.name);
                console.log(iterator.age);
            }
        }
    }

    /**取消预约 */
    unRePatien(p) { //医生中的删除
        let temp = this.patientList.indexOf(p); //找病人在列表中的下标位置
        this.patientList.splice(temp, 1); //删除patientList列表中temp对应的下标位置 就是删除这个元素:patientList[temp]
    }

    /**给病人看病 */
    GiveSeePatient(p) {
        // constructor(dept, doctor, patient, bingzheng, time) 
        let M = new Medicalhistory(this.dept, this, p, 'xxxxx症状', new Date()); //医生生成病历
        let P = new Prescription('药品1', '药品2', '药品3', '药品4', '药品5', '药品6'); //医生生成药方
        this.medicalhistoryList.push(M); //把病历存一份以后给自己
        this.prescriptionList.push(P); //把处方存一份以后给自己
        p.medicalhistory = M; //把病历也给病人一份
        p.prescription = P; //把处方也给病人一份
        // console.log(p.name);
    }

    /**医生查看病人病状 */
    showPatientMdeic(p) {
        let flag = 0;
        // console.log(p.name);
        // console.log(this.patientList[0].name);
        for (const iterator of this.patientList) {
            if (iterator.name == p.name) {
                flag = 1;
                iterator.medicalhistory.showMedicalhistory();
            }
        }
        if (flag == 0)
            console.log('没有该病人的病历信息,也可能是被删除了');
    }

    /**医生查看病人药方 */
    showPrescription(p) {
        let flag = 0;
        // console.log(p.name);
        // console.log(this.patientList[0].name);
        for (const iterator of this.patientList) {
            if (iterator.name == p.name) {
                flag = 1;
                iterator.prescription.showPrescription();
            }
        }
        if (flag == 0)
            console.log('没有该病人的处方信息');
    }
}


//患者病人
class Patient {
    registrationList = new Array();
    medicalhistory;
    prescription;
    constructor(name, age, ) {
        this.name = name;
        this.age = age;
    }
    show() {}

    /**预约医生 */
    registration(doct) {
        this.registrationList.push(doct); //将医生存入registrationList列表
        doct.beRegistration(this); //调用医生中的beRegistration方法进行双向绑定
    }

    /**查看预约了哪些医生 */
    showRegistration() {
        if (this.registrationList.length == 0) console.log('您还没有预约任何医生');
        else {
            console.log(`-----病人${this.name}的预约列表`);
            for (const iterator of this.registrationList) {
                console.log(iterator.doctorname);
            }
        }
    }

    /**取消预约 */
    unRegistration(doct) {
        let temp = this.registrationList.indexOf(doct); //找医生在列表中的位置

        if (temp == -1) {
            console.log('您没有预约该医生'); //没有找到
        } else {
            this.registrationList.splice(temp, 1); //找到了就删除
            doct.unRePatien(this); //删除中后调用医生中的删除把病人也删除了 双向删除
        }
    }

    /**看病 */
    seePatient(doct) {
        doct.GiveSeePatient(this);
    }

    /**看完病之后生成了病历 -- 查看病历 */
    showMedicalhistory() {
        this.medicalhistory.showMedicalhistory();
    }

    /**看完病之后生成了药方 -- 查看药方 */
    showPrescription() {
        this.prescription.showPrescription();
    }
}


//病历
class Medicalhistory {
    // new Medicalhistory(this.dept, this, pati, '这里解释病状一些信息').seveMedicalhistory();
    constructor(dept, doctor, patient, bingzheng, time) {
        this.dept = dept;
        this.doctor = doctor;
        this.patient = patient;
        this.bingzheng = bingzheng;
        this.time = time;
    }

    showMedicalhistory() {
        // console.log(this.patient);
        console.log(`--------病人${this.patient.name}的病历信息如下-------`);
        console.log(`科室:${this.dept.deptname}`);
        console.log(`主治医师:${this.doctor.doctorname}`);
        console.log(`病状:${this.bingzheng}`);
        console.log(`时间:${this.time}`);
        console.log(`其他.....`); //后期扩展
        console.log(`------------- END ------------`);
    }
}

//药方
class Prescription {
    constructor(...medicine) { //name, 
        // this.name = name;
        this.medicine = medicine;
    }
    showPrescription() {
        console.log(this.medicine);
    }
}

// //这是在做医院整体框架
// //医院总科室列表
let deptList = new Array();
// //内部科室
let internalList = new Array(); //内科医生列表
let surgeryList = new Array(); //外科医生列表
// let gynaecologyList = new Array(); //妇产科医生列表
// let pediatriList = new Array(); //儿科医生列表
// let emergencyList = new Array(); //急诊科医生列表
// //存放
let dept = new Department('内科', internalList); //创建科室 传入医生列表
//内科
let doctWang = new Doctor('王医生', dept);
let doctLi = new Doctor('李医生', dept);
let doctZhang = new Doctor('张医生', dept);
internalList.push(doctWang, doctLi, doctZhang);
// //外科
let dept2 = new Department('外科', surgeryList); //创建科室 传入医生列表
let doctZhao = new Doctor('赵医生', dept2);
let doctXiao = new Doctor('肖医生', dept2);
let doctHuo = new Doctor('霍医生', dept2);
surgeryList.push(doctZhao, doctXiao, doctHuo); //将外科医生存放到外科列表中
// //将所有的科室于医院进行绑定
let hospital = new Xxhospital('XX医院', deptList);
// //将所有科室放到列表中
deptList.push(dept, dept2); //目前只有两个科室

// hospital.showDeptAndDoct(); //打印医院总名单
// dept.showDoct(); //打印科室的医生
// doctZhang.belongDept();//查看医生属于那个科室

//接下来开始做病人与医生的模块
let p1 = new Patient('张三', 24, null); //病人p1
let p2 = new Patient('李四', 34, null); //病人p2
let p3 = new Patient('赵五', 44, null); //病人p3

p1.registration(doctWang); //病人预约医生 
// p1.registration(doctLi);
// p2.registration(doctWang);
// p1.registration(doctHuo);
// p1.showRegistration();
// doctWang.showRePatients();
// p1.showRegistration(); //查看病人预约的医生
// doctWang.showRePatients(); //查看医生中的预约病人
// p1.unRegistration(doctWang); //删除与该医生的预约
// p1.showRegistration(); //再次查看
// doctWang.showRePatients(); //再次查看
p1.seePatient(doctWang); //病人看病-->去哪个医生
// p1.showMedicalhistory();//病人查看自己的病历
// doctWang.showPatientMdeic(p1); //医生查看那个病人病历
p1.showPrescription(); //病人查看自己的处方
doctWang.showPrescription(p1); //医生查看那个病人处方
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值