首页 > 基础资料 博客日记

JavaSE基础知识分享(八)

2024-08-16 22:00:03基础资料围观183

这篇文章介绍了JavaSE基础知识分享(八),分享给大家做个参考,收藏Java资料网收获更多编程知识

写在前面

前面讲的是java中集合这部分的内容,今天给大家发一个上期题目参考答案!

Person类:

package com.shujia.TiMu_1000.ten2.Ti15;

/**
 * @author cjy
 * @create 2024-08-07-20:47
 */
public abstract class Person {
    private String name;
    private int age;
    private String gender;

    public Person() {
    }

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    private int setAge(int age) {
        if (age < 0) {
            System.out.println("年龄输入错误,默认为0");
            return 0;
        }
        return age;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

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

    @Override
    protected void finalize() throws Throwable {
        try {
            System.out.println("对象 " + name + " 正在被销毁");
        } finally {
            super.finalize();
        }
    }

    public void increaseAge(int age) {
        if (age > 0 && setAge(this.age) != 0) {
            this.age += age;
        } else {
            System.out.println("你增加的年龄为负数,不能增加!");
        }
    }

    public abstract String speak();
}

School类:

package com.shujia.TiMu_1000.ten2.Ti15;

/**
 * @author cjy
 * @create 2024-08-07-21:05
 */
public class School {
    public Person[] people;

    public School() {
    }

    public School(int size) {
        people = new Person[size];
    }

    public void print(int index) {
        if (index >= 0 && index < people.length && people[index] != null) {
            System.out.println(people[index].speak());
        } else {
            System.out.println("索引超出范围或对象为空");
        }
    }
}

Student类:

package com.shujia.TiMu_1000.ten2.Ti15;

/**
 * @author cjy
 * @create 2024-08-07-20:58
 */
public class Student extends Person{
    private String studentId;

    public Student() {
    }

    public Student(String name, int age, String gender, String studentId) {
        super(name, age, gender);
        this.studentId = studentId;
    }

    public String getStudentId() {
        return studentId;
    }

    public void setStudentId(String studentId) {
        this.studentId = studentId;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + getName() + '\'' +
                ", age=" + getAge() +
                ", gender='" + getGender() + '\'' +
                ", studentId='" + studentId + '\'' +
                '}';
    }

    @Override
    public String speak() {
        return getName()+"正在学高数!!";
    }

}

Teacher类:

package com.shujia.TiMu_1000.ten2.Ti15;

/**
 * @author cjy
 * @create 2024-08-07-21:04
 */
public class Teacher extends Person implements Smoking{
    public Teacher() {
    }

    public Teacher(String name, int age, String gender) {
        super(name, age, gender);
    }



    @Override
    public String speak() {
        return getName()+"正在教高数!!";
    }

    @Override
    public void smoking() {
        System.out.println(getName()+"正在吸烟");
    }

    public void preparing(){
        System.out.println(getName()+"正在备课");
    }
}


Test类:

package com.shujia.TiMu_1000.ten2.Ti15;

/**
 * @author cjy
 * @create 2024-08-07-21:01
 */
public class Test {
    public static void main(String[] args) {

//        Person p4 = new Person("小明",21,"男");
//        System.out.println(p4.toString());

//        Person p4 = new Person("小明", 20, "男");
//        System.out.println(p4.toString());
//        System.out.println("================================");
//        p4.increaseAge(2);
//        System.out.println(p4.toString());

//        Student p1 =new Student("李子杰",23,"男","1001");
//        System.out.println(p1.toString());


        Student p1 =new Student("李子杰",23,"男","1001");
        Student p2 =new Student("尤海涛",22,"男","1002");
        Teacher p3 =new Teacher("黄崇涛",21,"男");
        School s1 = new School(3);
        s1.people[0] = p1;
        s1.people[1] = p2;
        s1.people[2] = p3;
        System.out.println(p1.speak());
        System.out.println(p2.speak());
        System.out.println(p3.speak());
        Teacher t1 = (Teacher)  s1.people[2];
        t1.preparing();

//
//        p3.smoking();

    }
}


抽烟接口:

package com.shujia.TiMu_1000.ten2.Ti15;

/**
 * @author cjy
 * @create 2024-08-08-20:15
 */
public interface Smoking {
    abstract void smoking();
}

好了,今天的分享就结束了。答案也是仅供参考,不代表最终答案,如果有错误或更简单的方法,欢迎在下面留言!!


文章来源:https://www.cnblogs.com/cjybigdatablog/p/18363673
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!

标签:

相关文章

本站推荐

标签云