第十三题

代码
package com.quange.homework;
public class Homework13 {
public static void main(String[] args) {
// 打印信息
Student student = new Student("小明", 17, '男' , "20220101");
student.printInfo();
System.out.println("--------------------------------");
Teacher teacher = new Teacher("老韩", 40, '男', 20);
teacher.printInfo();
// 定义对象数组
Person[] people = new Person[4];
people[0] = new Student("小明", 18, '男', "20220101");
people[1] = new Student("小王", 17, '男', "20220102");
people[2] = new Teacher("老韩", 40, '男', 20);
people[3] = new Teacher("老王", 50, '男', 30);
// 创建对象
Homework13 homework13 = new Homework13();
homework13.bubbleSort(people);
// 输入排序后的对象
for (int i = 0; i < people.length; i++) {
System.out.println(people[i]);
}
System.out.println("----------------------------");
// 遍历数组,调用test方法
for (int i = 0; i < people.length; i++) {
homework13.test(people[i]);
}
}
// 方法,完成年龄的从大到小排序
public void bubbleSort(Person[] people){
// 定义变量
Person tmp = null;
for (int i = 0; i < people.length -1; i++) {
for (int j = 0; j < people.length -1 -i; j++) {
if (people[j].getAge() < people[j+1].getAge()){
tmp = people[j];
people[j] = people[j+1];
people[j+1] = tmp;
}
}
}
}
// 方法,这里使用向下转型和类型判断
public void test(Person p){
if (p instanceof Student){ // p的运行类型如果是Student
((Student) p).study();
}else if (p instanceof Teacher){ //p的运行类型如果是Teacher
((Teacher) p).teach();
}else {
System.out.println("do nothing....");
}
}
//父类 Person 类
static class Person{
// 共同属性
private String name; // 姓名
private int age; // 年龄
private char sex; // 性别
public Person(String name, int age, char sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
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 char getSex() {
return sex;
}
public void setSex(char sex) {
this.sex = sex;
}
// 共有的方法
public String play(){
return name + "爱玩";
}
public String basicInfo(){
return "姓名: " + name + "\n年龄:" + age + "\n性别:" + sex;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
'}';
}
}
// 子类 Stduent
static class Student extends Person{
// 特有属性
private String stu_id; // 学号
public Student(String name, int age, char sex, String stu_id) {
super(name, age, sex);
this.stu_id = stu_id;
}
// 特有方法
public void study(){
System.out.println(getName() + "承诺,我会好好学习");
}
@Override
public String play() {
return super.play() + "足球";
}
@Override
public String toString() {
return "Student{" +
"stu_id='" + stu_id + '\'' +
'}' + super.toString();
}
public void printInfo(){
System.out.println("学生的信息:");
System.out.println(super.basicInfo());
System.out.println("学号:" + stu_id);
study();
System.out.println(play());
}
}
// 子类 Teacher
static class Teacher extends Person{
// 特有属性
private int work_age; // 工龄
public Teacher(String name, int age, char sex, int work_age) {
super(name, age, sex);
this.work_age = work_age;
}
// 特有方法
public void teach(){
System.out.println( getName() + "承诺,我会认真教学。");
}
@Override
public String play() {
return super.play() + "象棋";
}
@Override
public String toString() {
return "Teacher{" +
"work_age=" + work_age +
'}' + super.toString();
}
public void printInfo(){
System.out.println("老师的信息:");
System.out.println(super.basicInfo());
System.out.println("工龄:" + work_age);
teach();
System.out.println(play());
}
}
}
输出
学生的信息:
姓名: 小明
年龄:17
性别:男
学号:20220101
小明承诺,我会好好学习
小明爱玩足球
--------------------------------
老师的信息:
姓名: 老韩
年龄:40
性别:男
工龄:20
老韩承诺,我会认真教学。
老韩爱玩象棋
Teacher{work_age=30}Person{name='老王', age=50, sex=男}
Teacher{work_age=20}Person{name='老韩', age=40, sex=男}
Student{stu_id='20220101'}Person{name='小明', age=18, sex=男}
Student{stu_id='20220102'}Person{name='小王', age=17, sex=男}
----------------------------
老王承诺,我会认真教学。
老韩承诺,我会认真教学。
小明承诺,我会好好学习
小王承诺,我会好好学习
进程已结束,退出代码0
评论区