class Person{
private String name;
private char sex;
private int age;
public Person(String name, char sex, int age) {
super();
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 showInfo(){
System.out.println("姓名:" + getName()) ;
System.out.println("年龄:" + getAge()) ;
System.out.println("性别:" + getSex()) ;
}
}
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() {
System.out.println("----------------");
System.out.println(getName()+ "爱玩足球");
return getName();
}
@Override
public void showInfo() {
System.out.println("学生的信息:");
super.showInfo();
System.out.println("学号:" + stu_id);
}
}
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() {
System.out.println("----------------");
System.out.println(getName()+ "爱玩象棋");
return getName();
}
@Override
public void showInfo() {
System.out.println("老师的信息:");
super.showInfo();
System.out.println("工龄:" + work_age);
}
}
public class Test {
public static void main(String[] args) {
//定义多态数组
Person[] persons = new Person[4];
persons[0] = new Teacher("张飞", '男', 30, 5);
persons[1] = new Teacher("张三", '男', 45, 7);
persons[2] = new Student("lili", '女', 12, "123");
persons[3] = new Student("aby", '男', 16, "456");
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()){
Person temp = persons[j];
persons[j] = persons[j+1];
persons[j+1] = temp;
}
}
}
for(int i = 0 ; i < persons.length; i++){
if(persons[i] instanceof Student){//判断类型 并向下转型
Student s = (Student) persons[i];
s.showInfo();
s.study();
s.play();
}else if(persons[i] instanceof Teacher){
Teacher t = (Teacher) persons[i];
t.showInfo();
t.teach();
t.play();
}else if(persons[i] instanceof Teacher){
Person p = (Person) persons[i];
p.showInfo();
}else{
System.out.println("输入类型有误,请重新输入");
}
}
}
}
我是A类
hahah我是B类的有参构造
我是C类的有参构造
我是C类的无参构造
- super(),即为继承父类的构造函数
- 继承之后,都是先执行父类的无参构造,再执行自己的有参构造
15.多态
>多态定义:
方法或对象具有多种形态,是面向对象(OOP)的第三大特征,是建立在封装和继承基础之上的
>多态具体体现:
- 方法多态:重载与重写
- 对象多态:对象的编译类型和运行类型可以不一致,编译类型在定义时就确定,不能变化;对象的运行类型时可以变化的,可通过getClass()来查看运行类型【左编译,右运行】
16.动态绑定机制
-调用对象方法时,该方法会和对象的内存地址/运行类型(等号右边)绑定
-调用对象属性时,没有动态绑定,哪里声明哪里使用