package com.Wzg;
public class Test13 {
public static void main(String[] args) {
System.out.println("================");
person1[] p=new person1[4];//创建父类数组
p[0]=new student("Morty",14,"男",1020);
p[1]=new student("Summer",17,"女",1022);
p[2]=new teacher1("Toddy",37,"男",13);
p[3]=new teacher1("BoJack",44,"男",22);
person1 temp;
for (int i= 0; i <p.length-1; i++) {//冒泡排序
for (int j= 0; j <p.length-1-i; j++) {
if(p[j].getAge()>p[j+1].getAge()){
temp=p[j];
p[j]=p[j+1];
p[j+1]=temp;
}
}
}
for (int i = 0; i <p.length; i++) {//输出排序后的数组
System.out.println(p[i].info());
System.out.println("================");
}
for (int i = 0; i <p.length; i++) {//调用指定对象中的私有方法
if(p[i] instanceof student){
student s=(student)p[i];//向下转型
System.out.println(s.stu());
}else {
teacher1 t=(teacher1)p[i];//向下转型
System.out.println(t.tea());
}
}
}
}
class person1{
private String name;
private int age;
private String sex;
public person1(String name, int age, String 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 String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String play(){
return name+"爱玩";
}
public String info(){
return "信息如下:\n"+"姓名:"+name+"\n年龄:"+age+"\n性别:"+sex;
}
}
class student extends person1{
private int stu_id;
public student(String name, int age, String sex, int stu_id) {
super(name, age, sex);
this.stu_id = stu_id;
}
public String stu(){
return "好好学习天天向上!";
}
public String play(){
return super.getName()+"爱玩足球";
}
public String info(){
return super.info()+"\n学号:"+stu_id+"\n"+stu()+"\n"+play();
}
}
class teacher1 extends person1{
private int work_age;
public teacher1(String name, int age, String sex, int work_age) {
super(name, age, sex);
this.work_age = work_age;
}
public String tea(){
return "子不教,父之过;教不严,师之惰!";
}
public String play(){
return super.getName()+"爱下象棋";
}
public String info(){
return super.info()+"\n工龄:"+work_age+"\n"+tea()+"\n"+play();
}
}