文章目录
什么是多态
如何实现多态
多态的示例操作
Person类
public class Person {
private String name;
private int age;
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 Person(String name, int age) {
this.name = name;
this.age = age;
}
public void printInfo() {
System.out.println("姓名:"+name+"\n年龄:"+age);
}
}
Student 类
public class Student extends Person {
private String department;
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public Student(String name, int age, String department) {
super(name, age);
this.department = department;
}
//方法重写
public void printInfo() {
super.printInfo();
System.out.println("系别:"+department);
}
}
Teacher 类
public class Teacher extends Person {
private String major;
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public Teacher(String name, int age, String major) {
super(name, age);
this.major = major;
}
//重写打印信息方法
public void printInfo() {
super.printInfo();
System.out.println("专长:"+major);
}
}
School类
public class School {
// //为学生开启门禁
// public void openDoor(Student stu) {
// stu.printInfo();
// System.out.println("允许进入");
// }
// //互为重载方法 参数列表不同
// //为教师开始门禁
// public void openDoor(Teacher t) {
// t.printInfo();
// System.out.println("允许进入!");
// }
//用多态实行方法
public void openDoor(Person p)
{
p.printInfo();
System.out.println("允许进入!");
}
}
测试类
public class EX8_2 {
public static void main(String[] args) {
Person p;//没有实体
Student stu =new Student("小红",23,"英语系");
p=stu;//p是stu的上转型对象
//p.setDepartment();//不能调用新增的方法
p.setName("小花");//可以调用继承的放啊
p.printInfo();//可以调用重写后的方法
Student stuNew =(Student)p;//需要进行强制类型转换
stuNew.setDepartment("中文系");
stuNew.printInfo();
}
}
Java实现多态有三个必要条件:继承、重写、向上转型。
instanceof
主要操作:
instanceof的示例操作:
Person类
public class Person {
private String name;
private int age;
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 Person(String name, int age) {
this.name = name;
this.age = age;
}
public void printInfo() {
System.out.println("姓名:"+name+"\n年龄:"+age);
}
}
Student类
public class Student extends Person {
private String department;
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public Student(String name, int age, String department) {
super(name, age);
this.department = department;
}
//方法重写
public void printInfo() {
super.printInfo();
System.out.println("系别:"+department);
}
public void haveClass(String course) {
System.out.println("我正在上"+course);
}
}
Teacher类
public class Teacher extends Person {
private String major;
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
public Teacher(String name, int age, String major) {
super(name, age);
this.major = major;
}
//重写打印信息方法
public void printInfo() {
super.printInfo();
System.out.println("专长:"+major);
}
//新增的方法
public void giveLesson() {
System.out.println("我正在上"+major+"课,学生听的很认真");
}
}
School 类
public class School {
// //为学生开启门禁
// public void openDoor(Student stu) {
// stu.printInfo();
// System.out.println("允许进入");
// }
// //互为重载方法 参数列表不同
// //为教师开始门禁
// public void openDoor(Teacher t) {
// t.printInfo();
// System.out.println("允许进入!");
// }
//用多态实行方法
public void openDoor(Person p)
{
p.printInfo();
System.out.println("允许进入!");
}
public void check(Person p) {
System.out.println("学校开始检查:");
if(p instanceof Student) {
((Student)p).haveClass("java");
}
if(p instanceof Teacher) {
((Teacher)p).giveLesson();
}
}
}
测试类
public class EX8_3 {
public static void main(String[] args) {
School sc=new School();
sc.check(new Student("小明",19,"信息工程学院"));
sc.check(new Teacher("张老师",35,"java"));
}
}