java12--本章作业

该文章展示了一个Java程序,包括Person类及其子类Student和Teacher,实现了基于年龄的冒泡排序。程序中探讨了类的继承、方法的覆盖以及多态的使用。同时,展示了如何根据对象类型调用不同的方法。

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

1. 第1题

public class Hello{
    public static void main(String[] args){
        Person[] persons = new Person[3];

        persons[0] = new Person("zsq", 18, "学生");
        persons[1] = new Person("zzq", 19, "大学生");
        persons[2] = new Person("zqq", 25, "研究生");

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

        bubble(persons);
        System.out.println("");
        for(int i = 0; i < persons.length; i++){

            System.out.println(persons[i].toString());
        }

    }

    public static void bubble(Person[] persons){
        Person temp = null;
//        for(int i = 0; i < objs.length; i++){
//            if(objs[i] instanceof Person){
//                objs[i] = (Person)objs[i];
//            }
//        }
        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;
                }
            }

        }

    }
}

class Person{
    private String name;
    private int age;
    private String job;

    public Person() {
    }

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

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

中间几道题目感觉都是一个类型的,就不做了

对于father而言:

super可以访问:name,g1()

this可以访问:id,score,f1(),this.g1(),this.name 这两个漏了

对于Son而言:

super可以访问:name, id, g1(),f1()

this可以访问:name,g1(),show(),id,f1(),"aa”是无法访问到的,Grand的g1()也无法访问到

 Test,demo,rose,jack,john,jack、

注意john那边是因为属性没有动态绑定机制,哪里声明就在哪里使用

class Point{
    double x;
    double y;
    public Point(double x, double y) {
        this.x = x;
        this.y = y;
    }
}
class LabeledPoint extends Point{
    String label;
    public LabeledPoint(String label,double x, double y){
        super(x, y);
        this.label = label;
    }

这一题太棒啦,总结得很好哦!

 

 很简单,只要注意B的有参构造第一句隐藏了super()

 这题比较综合,体现了面向对象封装,继承,多态的思想,好好做诶嘿嘿

public class Hello{
    public static void main(String[] args) {
        Person[] persons = new Person[4];
        persons[0] = new Student("zhy", '女', 17, "001");
        persons[1] = new Student("zsq", '男', 22, "002");
        persons[2] = new Teacher("ljw", '女', 19, 3);
        persons[3] = new Teacher("dxy", '女', 18, 2);
//        bubble(persons);
        for(int i = 0; i < persons.length; i++){
//            System.out.println(persons[i].toString());
            show(persons[i]);
        }




    }
    public static void bubble(Person[] persons){
        for(int i = 0; i < persons.length - 1; i++){
            for(int j = 0; j < persons.length - 1 - i; j ++){
                Person temp = null;
                if(persons[j].getAge() < persons[j+1].getAge()){
                    temp = persons[j];
                    persons[j] = persons[j+1];
                    persons[j+1] = temp;
                }
            }
        }
    }

    public static void show(Person person){
        if(person instanceof Student){
            ((Student) person).study();
        }else if(person instanceof Teacher){
            ((Teacher) person).teach();
        }
    }
}
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;
    }

    public String play(){
        return name + "爱玩";
    }

    public void BasicInfo(){
        System.out.println("姓名:" + name + "\n年龄:" + age + "\n性别:" + sex);
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", sex=" + sex +
                ", age=" + age +
                '}';
    }
}
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;
    }

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

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

    public void printInfo(){
        System.out.println("学生的信息:");
        super.BasicInfo();
        this.study();
        this.play();
    }

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

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

    public void teach(){
        System.out.println("我承诺,我会认真教学");
    }

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值