多态的应用
1.多态数组:数组的定义类型为父类类型,里面保存的实际元素类型为子类类型
2.多态参数:方法定义的形参类型为父类类型,实参允许为子类类型
public class Homework13 {
public static void main ( String[ ] args) {
Student student = new Student ( "小明" , '男' , 15 , "00023102" ) ;
student. printInfo ( ) ;
System. out. println ( "=======================" ) ;
Teacher teacher = new Teacher ( "老王" , '男' , 30 , 5 ) ;
teacher. printInfo ( ) ;
Person[ ] persons = new Person [ 4 ] ;
persons[ 0 ] = new Student ( "jack" , '男' , 10 , "0001" ) ;
persons[ 1 ] = new Student ( "mary" , '女' , 20 , "0002" ) ;
persons[ 2 ] = new Teacher ( "smith" , '男' , 36 , 5 ) ;
persons[ 3 ] = new Teacher ( "scoot" , '男' , 26 , 1 ) ;
Homework13 homework13 = new Homework13 ( ) ;
homework13. bubbleSort ( persons) ;
System. out. println ( "-----------排序后的数组-----------------" ) ;
for ( int i = 0 ; i < persons. length; i++ ) {
System. out. println ( persons[ i] ) ;
}
System. out. println ( "========================" ) ;
for ( int i = 0 ; i < persons. length; i++ ) {
homework13. test ( persons[ i] ) ;
}
}
public void test ( Person p ) {
if ( p instanceof Student ) {
( ( Student) p) . study ( ) ;
} else if ( p instanceof Teacher ) {
( ( Teacher) p) . teach ( ) ;
} else {
System. out. println ( "什么都不做!" ) ;
}
}
public void bubbleSort ( Person[ ] persons) {
Person tmp = null;
for ( int i = 0 ; i < persons. length - 1 ; i++ ) {
for ( int j = 0 ; j < persons. length - 1 - i; j++ ) {
if ( persons[ j] . getAge ( ) < persons[ j + 1 ] . getAge ( ) ) {
tmp = persons[ j] ;
persons[ j] = persons[ j + 1 ] ;
persons[ j + 1 ] = tmp;
}
}
}
}
}
class Person {
private String name;
private char gender;
private int age;
public Person ( String name, char gender, int age) {
this . name = name;
this . gender = gender;
this . age = age;
}
@Override
public String toString ( ) {
return "Person{" +
"name='" + name + '\'' +
", gender=" + gender +
", age=" + age +
'}' ;
}
public String basicInfo ( ) {
return "姓名:" + name + "\n年龄:" + age + "\n性别:" + gender;
}
public String play ( ) {
return name + "爱玩" ;
}
public String getName ( ) {
return name;
}
public void setName ( String name) {
this . name = name;
}
public char getGender ( ) {
return gender;
}
public void setGender ( char gender) {
this . gender = gender;
}
public int getAge ( ) {
return age;
}
public void setAge ( int age) {
this . age = age;
}
}
class Student extends Person {
private String stu_id;
public void study ( ) {
System. out. println ( getName ( ) + "承诺,我会认真学习!" ) ;
}
@Override
public String toString ( ) {
return "Student{" +
"stu_id='" + stu_id + '\'' +
'}' + super . toString ( ) ;
}
public void printInfo ( ) {
System. out. println ( "学生的信息:" ) ;
System. out. println ( basicInfo ( ) ) ;
System. out. println ( "学号:" + stu_id) ;
study ( ) ;
System. out. println ( play ( ) ) ;
}
@Override
public String play ( ) {
return super . play ( ) + "足球" ;
}
public Student ( String name, char gender, int age, String stu_id) {
super ( name, gender, 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;
}
}
class Teacher extends Person {
private int work_age;
public void teach ( ) {
System. out. println ( getName ( ) + "承诺,我会认真教学!" ) ;
}
@Override
public String toString ( ) {
return "Teacher{" +
"work_age=" + work_age +
'}' + super . toString ( ) ;
}
public void printInfo ( ) {
System. out. println ( "老师的信息:" ) ;
System. out. println ( basicInfo ( ) ) ;
System. out. println ( "工龄:" + work_age) ;
teach ( ) ;
System. out. println ( play ( ) ) ;
}
@Override
public String play ( ) {
return super . play ( ) + "象棋" ;
}
public Teacher ( String name, char gender, int age, int work_age) {
super ( name, gender, 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;
}
}