7.1、什么是继承【重点】
7.1.1 为什么需要继承?
我们通过一个案例来说明:实现学校管理系统,需要抽象出两个类,分别为老师和学生,老师类中包含姓名、年龄、性别、薪水四个属性、学生类中包含姓名、年龄、性别、专业四个属性,分别完成老师和学生的自我介绍、工作行为。代码如下:
public class Teacher { private String name; private int age; private String gender; private int salary; public void show() { System.out.println("\n--------显示信息---------\n"); System.out.println("姓名:" + this.name); System.out.println("年龄:" + this.age); System.out.println("性别:" + this.gender); System.out.println("薪水:" + this.salary); } public void work() { System.out.println("\n我叫" + this.name + ",我是一名老师,我的工作是授课!\n"); } //以下代码是Setter、Getter方法 …… } |
public class Student { private String name; private int age; private String gender; private String professional; public void show() { System.out.println("\n--------显示信息---------\n"); System.out.println("姓名:" + this.name); System.out.println("年龄:" + this.age); System.out.println("性别:" + this.gender); System.out.println("专业:" + this.professional); } public void work() { System.out.println("\n我叫" + this.name + ",我是一名学生,我的工作是学习!\n"); } //以下代码是Setter、Getter方法 …… } |
7.1.2 继承
观察上方的代码,我们发现有大量的重复代码,如果说现在增加一个保洁员的类,依然还需要姓名、年龄、性别这些属性、依然需要自我介绍和工作方法,代码依然存在大量的重复;学校员工种类越多,我们需要编写的重复代码也就越多。
由此得知,我们需要一种机制,将多个类的共性提取到一个类中(父类),让这些类(子类)共享相同属性与行为,这种机制叫做继承。
接下来,我们使用继承完成上面的案例,代码如下:
public class Employee { protected String name; protected int age; protected String gender; public void show() { System.out.println("\n--------显示信息---------\n"); System.out.println("姓名:" + this.name); System.out.println("年龄:" + this.age); System.out.println("性别:" + this.gender); } //以下代码是Setter、Getter方法 …… } public class Teacher extends Employee { private int salary; public void show() { super.show(); System.out.println("薪水:" + this.salary); } public void work() { System.out.println("\n我叫" + super.name + ",我是一名老师,我的工作是授课!\n"); } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } } public class Student extends Employee { private String professional; public void show() { super.show(); System.out.println("专业:" + this.professional); } public void work() { System.out.println("\n我叫" + super.name + ",我是一名学生,我的工作是学习!\n"); } public String getProfessional() { return professional; } public void setProfessional(String professional) { this.professional = professional; } } |
父类:又叫做“基类”或者“根类”。
子类:又叫做“派生类”。
extends关键字:继承,格式:class 类名 extends 父类。
super关键字:引用父类的成员。格式如下:
//引用构造方法: super(参数列表) //引用方法: super.方法名(参数列表) //引用属性: super.属性名 |
继承的好处:继承的本质是为了消除代码的冗余,提升了代码的重用性、可维护性。
7.1.3、继承特点
子类继承父类属性与行为。
构造方法不能继承,但是可以调用父类的构造方法。
子类在实例化的时候,当我们没有明确指出调用父类哪个构造方法时,调用父类的默认构造方法。
子类在实例化的时候,可以指定调用父类的相应构造方法。
正确的编写构造方法的示例:
//父类构造的编写格式: public Employee() { } public Employee(String name, int age, String gender) { this.name = name; this.age = age; this.gender = gender; } //子类构造的编写格式: public Student() {} public Student(String name, int age, String gender, String professional) { super(name, age, gender); this.professional = professional; } |
7.2、访问修饰符【重点】【面试点】
7.3、继承中的is a的关系【难点】
案例:将学校中所有的员工储存到数组中,循环遍历输出每个员工的信息,并调用员工工作方法,代码如下:
public class TestSchool { public static void main(String[] args) { Teacher t1 = new Teacher("王刚", 65, "男", 50000); Student s1 = new Student("刘德华", 60, "男", "古董甄别"); Employee[] employees = new Employee[2]; employees[0] = t1; employees[1] = s1; for (int i = 0; i < employees.length; i++) { if (employees[i] instanceof Teacher) { Teacher obj = (Teacher) employees[i]; obj.show(); obj.work(); } if (employees[i] instanceof Student) { Student obj = (Student) employees[i]; obj.show(); obj.work(); } } } } |
子类 is a 父类(即:子类与父类之间必须满足is a 的关系)。
典型应用:父类对象引用子类实例,这是多态的一种表现形式。
instanceof 关键字:判断对象是否为某个类的实例。格式:
if (employees[i] instanceof Teacher) { Teacher obj = (Teacher) employees[i]; obj.show(); obj.work(); } |