JAVA-D29

0342 - 0358 本章作业

第一题

package com.homework;

/*
定义person对象,对其年龄进行排序,以前是对基本属性排序,现在是对对象某一属性排序
 */
public class Homework01 {
    public static void main(String[] args) {
        //初始化person数组,创建三个对象
        Person[] persons = new Person[3];
        persons[0] = new Person("jack",15,"学生");
        persons[1] = new Person("tom",19,"流浪歌手");
        persons[2] = new Person("jerry",25,"研究生");
        Person temp = null;

        //1.输出当前的对象数组,俺冒泡排序由age从大到小的排序
        //2.按照名字长度从大到小
        /*for(int i = 0;i < persons.length - 1 ; i++){
            for (int j = 0; j < persons.length - i - 1; j++){
                if (persons[j].getAge() < persons[j+1].getAge()){
                    temp = persons[j];
                    persons[j] = persons[j+1];
                    persons[j+1] = temp;
                }
            }
        }*/

        for(int i = 0;i < persons.length - 1 ; i++){
            for (int j = 0; j < persons.length - i - 1; j++){
                if (persons[j].getName().length() < persons[j+1].getName().length()){
                    temp = persons[j];
                    persons[j] = persons[j+1];
                    persons[j+1] = temp;
                }
            }
        }

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

        //System.out.println(persons[0]);
    }
}

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;
    }

    //toString方法已经重写了,所以输出Person[i]会将person的全部属性输出
    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", job='" + job + '\'' +
                '}';
    }


}

第二题

四种访问修饰符即各自的权限。

本类同包子类不同包
public可以访问可以可以可以
protected可以可以可以不可以
默认可以可以不可以不可以
private可以不可以不可以不可以

第三题

package com.homework;
/*
3.编写老师类HomeworkO3.java
(1)要求有属性“姓名name”,“年龄age”,“职称post”,“基本工资salary"
(2)编写业务方法introduce(),实现输出一个教师的信息。
(3)编写教师类的三个子类:教授类(Professor)、副教授类、讲师类。工资级别分别为:教授为1.3、副教授为1.2、讲师类1.1。在三个子类里面都重写父类的introduce()方法。
(4)定义并初始化一个老师对象,调用业务方法,实现对象基本信息的后台打印。
*/
public class Homework03 {
    public static void main(String[] args) {
        professer p1 = new professer("张工", 48, "高级", 25000);
        p1.introduce();
    }
}
class teacher{
    private String name;
    private int age;
    private String post;
    private double salary;

    public teacher(String name, int age, String post, double salary) {
        this.name = name;
        this.age = age;
        this.post = post;
        this.salary = 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 void introduce(){
        System.out.println("姓名:"  + name + " 年龄:"
                + age + " 职称:" + post + " 薪水:" + salary);
    }

}
package com.homework;

//子类
public class professer extends teacher {
    //特有属性可自己增加...
    public professer(String name, int age, String post, double salary) {
        super(name, age, post, salary);
    }

    @Override
    public void introduce() {
        System.out.println("教授的信息:");
        super.introduce();
    }
}

第四题:

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值