文章目录
- 什么是多态
- 如何实现多态
- 多态的示例操作
- Person类
- Student 类
- Teacher 类
- School类
- 测试类
- Java实现多态有三个必要条件:继承、重写、向上转型。
- instanceof
- instanceof的示例操作:
- Person类
- Student类
- Teacher类
- School 类
- 测试类
什么是多态
如何实现多态
多态的示例操作
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(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.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(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"));
}
}