面向对象练习

OOP练习在这里插入图片描述

测试类

package com.zzj.practiseOOP.homework13;

import com.zzj.plolyarr.person;

public class HomeWork13 {
    public static void main(String[] args) {
        Person[] people = new Person[4];
        people[0] = new Student("张三", '男', 19, "qh1234");
        people[1] = new Student("李四", '女', 12, "qh5678");
        people[2] = new Teacher("小明", '男', 55, 20);
        people[3] = new Teacher("小王", '女', 65, 30);

        HomeWork13 homework13 = new HomeWork13();
        homework13.bubbleSort(people);

        for (int i = 0; i < people.length; i++) {
            System.out.println(people[i]);
        }

        for (int i = 0; i < people.length; i++) {
            homework13.test(people[i]);
        }

    }
    public void bubbleSort(Person[] people) {
        Person temp;
        for (int i = 0; i < people.length - 1; i++) {
            for (int j = 0; j < people.length - i -1; j++) {
                if (people[j].getAge() < people[j + 1].getAge() ) {
                    temp = people[j + 1];
                    people[j + 1] = people[j];
                    people[j] = temp;
                }
            }
        }
    }
    public void test(Person p) {
        if (p instanceof Student) { //p的运行内存是Student
            ((Student)p).study();
        } else if (p instanceof Teacher) {
            ((Teacher)p).teach();
        } else {
            System.out.println("…………");
        }
    }
}

父类

package com.zzj.practiseOOP.homework13;

public abstract class Person {
    private String name;
    private char sex;
    private int age;

    public Person(String name, char sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public char getSex() {
        return sex;
    }

    public void setSex(char sex) {
        this.sex = sex;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", sex=" + sex +
                ", age=" + age +
                '}';
    }

    public String play() {
        return name + "爱玩";
    }
    public String basicInfo() {
        return "姓名:" + name + "\n性别:" + sex + "\n年龄:" + age;
    }
    public void information() {

    }
}

老师子类

package com.zzj.practiseOOP.homework13;

public class Teacher extends Person{
    private int work_age;

    public Teacher(String name, char sex, int age, int work_age) {
        super(name, sex, age);
        this.work_age = work_age;
    }

    public int getWork_age() {
        return work_age;
    }

    public void setWork_age(int work_age) {
        this.work_age = work_age;
    }

    @Override
    public String toString() {
        return "Teacher{" + super.toString() +
                "work_age=" + work_age +
                '}';
    }

    public void teach() {
        System.out.println("我承诺!我会好好教学!");
    }

    @Override
    public String play() {
        return super.play() + "象棋";
    }
    public void printInfo() {
        System.out.println("老师的信息:");
        System.out.println(super.basicInfo());
        System.out.println("工龄:" + work_age);
        System.out.println(this.play());
        this.teach();
        System.out.println("====================");
    }
}

学生子类

package com.zzj.practiseOOP.homework13;

public class Student extends Person{
    private String stu_id;

    public Student(String name, char sex, int age, String stu_id) {
        super(name, sex, age);
        this.stu_id = stu_id;
    }

    public String getStu_id() {
        return stu_id;
    }

    public void setStu_id(String stu_id) {
        this.stu_id = stu_id;
    }

    @Override
    public String toString() {
        return "Student{" + super.toString() +
                "stu_id='" + stu_id + '\'' +
                '}';
    }


    public void study() {
        System.out.println("我承诺!我会好好学习。");
    }

    @Override
    public String play() {
        return super.play() + "足球";

    }
    public void printInfo() {
        System.out.println("学生的信息:");
        System.out.println(super.basicInfo());
        System.out.println("学号:" + stu_id);
        System.out.println(this.play());
        this.study();
        System.out.println("====================");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值