本章作业(面向对象高级)

本文档介绍了如何定义一个Person类并按年龄排序,创建包含姓名、年龄和职称的教师类,以及教授、副教授和讲师的子类。每个子类具有不同的工资比例,并重写了introduce()方法来展示教师信息。
摘要由CSDN通过智能技术生成

(1)定义一个Person类{name,age,job},初始化Person对象数组,有三个person对象,并按照age从大到小进行排序,使用冒泡排序

public class Test {
    public static void main(String[] args) {
        Person[] person = new Person[3];
        person[0] = new Person("jack", 18, "it");
        person[1] = new Person("karry", 22, "idol");
        person[2] = new Person("cyn", 20, "chairman");

        //从大到小排序:
        for (int i = 0; i < person.length-i; i++) {
            for (int j  = 0; j < person.length-i-1;j++){
                if (person[j].getAge()<person[j+1].getAge()){
                    Person person1 = new Person("",0,"");
                    person1 = person[j+1];
                    person[j+1] = person[j];
                    person[j] = person1;
                }
            }
        }
        for (int i = 0; i < person.length; i++) {
            System.out.println(person[i].getName()+"\t"+person[i].getAge()+"\t"+person[i].getJob());
        }
    }
}
class Person {
    private String name;
    private int age;
    private String job;

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getJob() {
        return job;
    }

    public void setJob(String job) {
        this.job = job;
    }
}

 

(2)编写老师类

(1)要求有属性“姓名name”,“年龄age”,“职称”,“基本工资salary”

(2)编写业务方法,introduce(),实现输出一个教室的信息

(3)编写教师类的三个子类:教授类,副教授类,讲师类。工资级别分别为:教授为1.3,副教授为1.2,讲师类1.1.在三个子类里都重写父类的introduce()方法。

(4)定义并初始化一个老师对象,调用业务方法,实现对象基本信息的后台打印

  • Teacher类:

public class Teacher {
    private String name;
    private int age;
    private String post;
    private double salary;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

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

    public String getPost() {
        return post;
    }

    public void setPost(String post) {
        this.post = post;
    }

    public double getSalary() {
        return salary;
    }

    public void setSalary(double salary) {
        this.salary = salary;
    }

    public C(String name, int age, String post, double salary) {
        this.name = name;
        this.age = age;
        this.post = post;
        this.salary = salary;
    }
    public void introduce(){
        System.out.print("姓名"+name+"\t"+"年龄"+age+"\t"+"职称"+post+"\t"+"工资"+salary);
    }
}
  • 教授类:

public class A extends Teacher {
    private double score;

    public A(String name, int age, String post, double salary, double score) {
        super(name, age, post, salary);
        this.score = score;
    }

    public void introduce(){
        super.introduce();
        System.out.println("\t级别"+score);
    }
}

.....其余类和教授类一样

测试类:

public class Test{
    public static void main(String[] args){
        A a = new A("JACK",55,"教授",10000,1.4);
        a.introduce();
    }
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值