1、继承的概念

继承是java面向对象编程技术的一块基石,因为它允许创建分等级层次的类。
继承就是子类继承父类的特征和行为,使得子类对象(实例)具有父类的实例域和方法,或子类从父类继承方法,使得子类具有父类相同的行为。

1.1、生活中的继承:

07_面向对象 - 图1
兔子和羊属于食草动物类,狮子和豹属于食肉动物类。
食草动物和食肉动物又是属于动物类。
所以继承需要符合的关系是:is-a,父类更通用,子类更具体。
虽然食草动物和食肉动物都是属于动物,但是两者的属性和行为上有差别,所以子类会具有父类的一般特性也会具有自身的特性。

2、 类的继承

面向对象的三大特性:

  1. 封装(封装细节)
  2. 继承
  3. 多态
  • 继承是面向对象的重要概念,软件中的继承和现实中的继承概念是一样的
  • 继承是实现软件可重用性的重要手段,如:A继承B,A就拥有了B的所有公开的特性
  • java中只支持类的单继承,也就是说A只能继承B,A不能同时继承C

java中的继承使用extends 关键字,语法格式:
[修饰符] class 子类 extends 父类{

}
【代码示例】

  1. public class ExtendsTest01 {
  2. public static void main(String[] args) {
  3. //如果参数比较多尽量使用setter方法,不要使用构造函数
  4. //因为参数比较多,构造函数表达的不是很明确
  5. Student student = new Student();
  6. student.setId(1001);
  7. student.setName("张三");
  8. student.setSex(true);
  9. student.setAddress("北京");
  10. student.setAge(20);
  11. student.setClassesId(10);
  12. System.out.println("id=" + student.getId());
  13. System.out.println("name=" + student.getName());
  14. System.out.println("sex=" + student.getSex());
  15. System.out.println("address=" + student.getAddress());
  16. System.out.println("age=" + student.getAge());
  17. System.out.println("classid=" + student.getClassesId());
  18. System.out.println("");
  19. System.out.println("");
  20. Employee emp = new Employee();
  21. emp.setId(1002);
  22. emp.setName("李四");
  23. emp.setSex(true);
  24. emp.setAddress("上海");
  25. emp.setAge(30);
  26. emp.setWorkYear(10);
  27. System.out.println("id=" + emp.getId());
  28. System.out.println("name=" + emp.getName());
  29. System.out.println("sex=" + emp.getSex());
  30. System.out.println("address=" + emp.getAddress());
  31. System.out.println("age=" + emp.getAge());
  32. System.out.println("workYear=" + emp.getWorkYear());
  33. }
  34. }
  35. class Student {
  36. //学号
  37. private int id;
  38. //姓名
  39. private String name;
  40. //性别
  41. private boolean sex;
  42. //地址
  43. private String address;
  44. //年龄
  45. private int age;
  46. //班级编号
  47. private int classesId;
  48. /*
  49. public Student(int id, String name, boolean sex, String address, int age) {
  50. this.id = id;
  51. this.name = name;
  52. this.sex = sex;
  53. this.address = address;
  54. this.age = age;
  55. }
  56. */
  57. //设置学号
  58. public void setId(int studentId) {
  59. id = studentId;
  60. }
  61. //读取学号
  62. public int getId() {
  63. return id;
  64. }
  65. public void setName(String studentName) {
  66. name = studentName;
  67. }
  68. public String getName() {
  69. return name;
  70. }
  71. public void setSex(boolean studentSex) {
  72. sex = studentSex;
  73. }
  74. public boolean getSex() {
  75. return sex;
  76. }
  77. public void setAddress(String studentAddress) {
  78. address = studentAddress;
  79. }
  80. public String getAddress() {
  81. return address;
  82. }
  83. public void setAge(int studentAge) {
  84. if (studentAge >=0 && studentAge <=120) {
  85. age = studentAge;
  86. }
  87. }
  88. public int getAge() {
  89. return age;
  90. }
  91. public void setClassesId(int classesId) {
  92. this.classesId = classesId;
  93. }
  94. public int getClassesId() {
  95. return classesId;
  96. }
  97. }
  98. class Employee {
  99. //员工编号
  100. private int id;
  101. //姓名
  102. private String name;
  103. //性别
  104. private boolean sex;
  105. //地址
  106. private String address;
  107. //年龄
  108. private int age;
  109. //工作年限
  110. private int workYear;
  111. //设置学号
  112. public void setId(int studentId) {
  113. id = studentId;
  114. }
  115. //读取学号
  116. public int getId() {
  117. return id;
  118. }
  119. public void setName(String studentName) {
  120. name = studentName;
  121. }
  122. public String getName() {
  123. return name;
  124. }
  125. public void setSex(boolean studentSex) {
  126. sex = studentSex;
  127. }
  128. public boolean getSex() {
  129. return sex;
  130. }
  131. public void setAddress(String studentAddress) {
  132. address = studentAddress;
  133. }
  134. public String getAddress() {
  135. return address;
  136. }
  137. public void setAge(int studentAge) {
  138. if (studentAge >=0 && studentAge <=120) {
  139. age = studentAge;
  140. }
  141. }
  142. public int getAge() {
  143. return age;
  144. }
  145. public void setWorkYear(int workYear) {
  146. this.workYear = workYear;
  147. }
  148. public int getWorkYear() {
  149. return workYear;
  150. }
  151. }

以上输出是完全正确的,但程序上存在一些冗余,因为Student和Employee都重复出现了id,name,address,sex,age属性,而Student和Employee其实都是Person,所以应该具有person的相关属性,所以完全可以抽取出一个Person类出来,让Student和Employee继承它,这样我们的冗余代码就会减少

【代码示例】,提取Person类,让Student和Employee继承Person

  1. public class ExtendsTest02 {
  2. public static void main(String[] args) {
  3. Student student = new Student();
  4. student.setId(1001);
  5. student.setName("张三");
  6. student.setSex(true);
  7. student.setAddress("北京");
  8. student.setAge(20);
  9. student.setClassesId(10);
  10. System.out.println("id=" + student.getId());
  11. System.out.println("name=" + student.getName());
  12. System.out.println("sex=" + student.getSex());
  13. System.out.println("address=" + student.getAddress());
  14. System.out.println("age=" + student.getAge());
  15. System.out.println("classid=" + student.getClassesId());
  16. System.out.println("");
  17. System.out.println("");
  18. Employee emp = new Employee();
  19. emp.setId(1002);
  20. emp.setName("李四");
  21. emp.setSex(true);
  22. emp.setAddress("上海");
  23. emp.setAge(30);
  24. emp.setWorkYear(10);
  25. System.out.println("id=" + emp.getId());
  26. System.out.println("name=" + emp.getName());
  27. System.out.println("sex=" + emp.getSex());
  28. System.out.println("address=" + emp.getAddress());
  29. System.out.println("age=" + emp.getAge());
  30. System.out.println("workYear=" + emp.getWorkYear());
  31. }
  32. }
  33. class Person {
  34. //姓名
  35. private String name;
  36. //性别
  37. private boolean sex;
  38. //地址
  39. private String address;
  40. //年龄
  41. private int age;
  42. //设置学号
  43. public void setId(int studentId) {
  44. id = studentId;
  45. }
  46. //读取学号
  47. public int getId() {
  48. return id;
  49. }
  50. public void setName(String studentName) {
  51. name = studentName;
  52. }
  53. public String getName() {
  54. return name;
  55. }
  56. public void setSex(boolean studentSex) {
  57. sex = studentSex;
  58. }
  59. public boolean getSex() {
  60. return sex;
  61. }
  62. public void setAddress(String studentAddress) {
  63. address = studentAddress;
  64. }
  65. public String getAddress() {
  66. return address;
  67. }
  68. public void setAge(int studentAge) {
  69. if (studentAge >=0 && studentAge <=120) {
  70. age = studentAge;
  71. }
  72. }
  73. public int getAge() {
  74. return age;
  75. }
  76. }
  77. class Student extends Person {
  78. //学号
  79. private int sid;
  80. //班级编号
  81. private int classesId;
  82. public void setClassesId(int classesId) {
  83. this.classesId = classesId;
  84. }
  85. public int getClassesId() {
  86. return classesId;
  87. }
  88. }
  89. class Employee extends Person {
  90. //编号
  91. private int eno;
  92. //工作年限
  93. private int workYear;
  94. public void setWorkYear(int workYear) {
  95. this.workYear = workYear;
  96. }
  97. public int getWorkYear() {
  98. return workYear;
  99. }
  100. }

运行结果和前面的一样,先从代码数量上来看,比以前少多了,没有重复了,软件设计有一个原则“重复的代码最好不要出现两次或多次”,如果出现两次以上相同的代码,那么就不好维护了,如果要改的话,那么多处都要改,给维护带来不变,而从我们的这个示例,大家看到采用继承可以达到重用性,把父类中的所有属性都继承下来了。

注:继承是对象级别的继承,将父类对象属性、方法继承过来
静态信息是存储在方法区,可以直接通过类名调用,在对象之前创建,所以不是对象级别的,更不能被继承

3、 方法的覆盖(Override)

首先看一下方法重载(Overload),回顾方法重载的条件:

  • 方法名称相同
  • 方法参数类型、个数、顺序至少有一个不同
  • 方法的返回类型可以不同,因为方法重载和返回类型没有任何关系
  • 方法的修饰符可以不同,因为方法重载和修饰符没有任何关系
  • 方法重载只出现在同一个类中

方法的覆盖(Override)的条件:

  1. 必须要有继承关系
  2. 覆盖只能出现在子类中,如果没有继承关系,不存在覆盖,只存在重载
  3. 在子类中被覆盖的方法,必须和父类中的方法完全一样,也就是方法名,返回类型、参数列表,完全一样
  4. 子类方法的访问权限不能小于父类方法的访问权限
  5. 子类方法不能抛出比父类方法更多的异常,但可以抛出父类方法异常的子异常
  6. 父类的静态方法不能被子类覆盖
  7. 父类的私有方法不能覆盖
  8. 覆盖是针对成员方法,而非属性

为什么需要覆盖?
就是要改变父类的行为。

1、对成员方法覆盖

【代码示例】,继承父类方法,不覆盖

  1. public class OverrideTest01 {
  2. public static void main(String[] args) {
  3. Student student = new Student();
  4. student.setId(1001);
  5. student.setName("张三");
  6. student.setSex(true);
  7. student.setAddress("北京");
  8. student.setAge(20);
  9. student.setClassesId(10);
  10. student.printInfo();
  11. System.out.println("");
  12. Employee emp = new Employee();
  13. emp.setId(1002);
  14. emp.setName("李四");
  15. emp.setSex(true);
  16. emp.setAddress("上海");
  17. emp.setAge(30);
  18. emp.setWorkYear(10);
  19. emp.printInfo();
  20. }
  21. }
  22. class Person {
  23. //学号
  24. private int id;
  25. //姓名
  26. private String name;
  27. //性别
  28. private boolean sex;
  29. //地址
  30. private String address;
  31. //年龄
  32. private int age;
  33. public void printInfo() {
  34. System.out.println("id=" + id + ", name=" + name + ",sex=" + sex + ", address=" + address + ", age=" + age);
  35. }
  36. //设置学号
  37. public void setId(int studentId) {
  38. id = studentId;
  39. }
  40. //读取学号
  41. public int getId() {
  42. return id;
  43. }
  44. public void setName(String studentName) {
  45. name = studentName;
  46. }
  47. public String getName() {
  48. return name;
  49. }
  50. public void setSex(boolean studentSex) {
  51. sex = studentSex;
  52. }
  53. public boolean getSex() {
  54. return sex;
  55. }
  56. public void setAddress(String studentAddress) {
  57. address = studentAddress;
  58. }
  59. public String getAddress() {
  60. return address;
  61. }
  62. public void setAge(int studentAge) {
  63. if (studentAge >=0 && studentAge <=120) {
  64. age = studentAge;
  65. }
  66. }
  67. public int getAge() {
  68. return age;
  69. }
  70. }
  71. class Student extends Person {
  72. //班级编号
  73. private int classesId;
  74. public void setClassesId(int classesId) {
  75. this.classesId = classesId;
  76. }
  77. public int getClassesId() {
  78. return classesId;
  79. }
  80. }
  81. class Employee extends Person {
  82. //工作年限
  83. private int workYear;
  84. public void setWorkYear(int workYear) {
  85. this.workYear = workYear;
  86. }
  87. public int getWorkYear() {
  88. return workYear;
  89. }
  90. }

1646726843(1).png

【代码示例】,继承父类方法,覆盖父类中的方法(改变父类的行为)

  1. public class OverrideTest02 {
  2. public static void main(String[] args) {
  3. Student student = new Student();
  4. student.setId(1001);
  5. student.setName("张三");
  6. student.setSex(true);
  7. student.setAddress("北京");
  8. student.setAge(20);
  9. student.setClassesId(10);
  10. student.printInfo();
  11. System.out.println("");
  12. Employee emp = new Employee();
  13. emp.setId(1002);
  14. emp.setName("李四");
  15. emp.setSex(true);
  16. emp.setAddress("上海");
  17. emp.setAge(30);
  18. emp.setWorkYear(10);
  19. emp.printInfo();
  20. }
  21. }
  22. class Person {
  23. //学号
  24. private int id;
  25. //姓名
  26. private String name;
  27. //性别
  28. private boolean sex;
  29. //地址
  30. private String address;
  31. //年龄
  32. private int age;
  33. public void printInfo() {
  34. System.out.println("id=" + id + ", name=" + name + ",sex=" + sex + ", address=" + address + ", age=" + age);
  35. }
  36. //设置学号
  37. public void setId(int studentId) {
  38. id = studentId;
  39. }
  40. //读取学号
  41. public int getId() {
  42. return id;
  43. }
  44. public void setName(String studentName) {
  45. name = studentName;
  46. }
  47. public String getName() {
  48. return name;
  49. }
  50. public void setSex(boolean studentSex) {
  51. sex = studentSex;
  52. }
  53. public boolean getSex() {
  54. return sex;
  55. }
  56. public void setAddress(String studentAddress) {
  57. address = studentAddress;
  58. }
  59. public String getAddress() {
  60. return address;
  61. }
  62. public void setAge(int studentAge) {
  63. if (studentAge >=0 && studentAge <=120) {
  64. age = studentAge;
  65. }
  66. }
  67. public int getAge() {
  68. return age;
  69. }
  70. }
  71. class Student extends Person {
  72. //班级编号
  73. private int classesId;
  74. public void setClassesId(int classesId) {
  75. this.classesId = classesId;
  76. }
  77. public int getClassesId() {
  78. return classesId;
  79. }
  80. public void printInfo() {
  81. System.out.println("id=" + getId() + ", name=" + getName() + ",sex=" + getSex() + ", address=" + getAddress() + ", age=" + getAge() + ",classesid=" + classesId);
  82. }
  83. }
  84. class Employee extends Person {
  85. //工作年限
  86. private int workYear;
  87. public void setWorkYear(int workYear) {
  88. this.workYear = workYear;
  89. }
  90. public int getWorkYear() {
  91. return workYear;
  92. }
  93. public void printInfo() {
  94. System.out.println("id=" + getId() + ", name=" + getName() + ",sex=" + getSex() + ", address=" + getAddress() + ", age=" + getAge() + ", workYear=" + workYear);
  95. }
  96. }

1646728183(1).png

以上子类对父类的方法进行了覆盖,改变了父类的行为,当我们new子类的时候,它不会再调用父类的方法了,而直接调用子类的方法,所以我们就完成了对父类行为的扩展。
【代码示例】,改善以上示例

  1. public class OverrideTest03 {
  2. public static void main(String[] args) {
  3. //Student student = new Student();
  4. //此种写法是错误的,Person不是Student
  5. //Student person_student = new Person();
  6. //此种写法是正确的,Student是Person
  7. Person person_student = new Student();
  8. person_student.setId(1001);
  9. person_student.setName("张三");
  10. person_student.setSex(true);
  11. person_student.setAddress("北京");
  12. person_student.setAge(20);
  13. //编译出错,因为Person看不到子类Student的属性
  14. //person_student.setClassesId(10);
  15. person_student.printInfo();
  16. System.out.println("");
  17. //Employee person_emp = new Employee();
  18. Person person_emp = new Employee();
  19. person_emp.setId(1002);
  20. person_emp.setName("李四");
  21. person_emp.setSex(true);
  22. person_emp.setAddress("上海");
  23. person_emp.setAge(30);
  24. //编译出错,因为Person看不到子类Employee的属性
  25. //person_emp.setWorkYear(10);
  26. person_emp.printInfo();
  27. }
  28. }
  29. class Person {
  30. //学号
  31. private int id;
  32. //姓名
  33. private String name;
  34. //性别
  35. private boolean sex;
  36. //地址
  37. private String address;
  38. //年龄
  39. private int age;
  40. public void printInfo() {
  41. System.out.println("id=" + id + ", name=" + name + ",sex=" + sex + ", address=" + address + ", age=" + age);
  42. }
  43. //设置学号
  44. public void setId(int studentId) {
  45. id = studentId;
  46. }
  47. //读取学号
  48. public int getId() {
  49. return id;
  50. }
  51. public void setName(String studentName) {
  52. name = studentName;
  53. }
  54. public String getName() {
  55. return name;
  56. }
  57. public void setSex(boolean studentSex) {
  58. sex = studentSex;
  59. }
  60. public boolean getSex() {
  61. return sex;
  62. }
  63. public void setAddress(String studentAddress) {
  64. address = studentAddress;
  65. }
  66. public String getAddress() {
  67. return address;
  68. }
  69. public void setAge(int studentAge) {
  70. if (studentAge >=0 && studentAge <=120) {
  71. age = studentAge;
  72. }
  73. }
  74. public int getAge() {
  75. return age;
  76. }
  77. }
  78. class Student extends Person {
  79. //班级编号
  80. private int classesId;
  81. public void setClassesId(int classesId) {
  82. this.classesId = classesId;
  83. }
  84. public int getClassesId() {
  85. return classesId;
  86. }
  87. public void printInfo() {
  88. System.out.println("id=" + getId() + ", name=" + getName() + ",sex=" + getSex() + ", address=" + getAddress() + ", age=" + getAge() + ",classesid=" + classesId);
  89. }
  90. }
  91. class Employee extends Person {
  92. //工作年限
  93. private int workYear;
  94. public void setWorkYear(int workYear) {
  95. this.workYear = workYear;
  96. }
  97. public int getWorkYear() {
  98. return workYear;
  99. }
  100. public void printInfo() {
  101. System.out.println("id=" + getId() + ", name=" + getName() + ",sex=" + getSex() + ", address=" + getAddress() + ", age=" + getAge() + ", workYear=" + workYear);
  102. }
  103. }

【代码示例】,改善以上示例

  1. public class OverrideTest04 {
  2. public static void main(String[] args) {
  3. Person person_student = new Student();
  4. person_student.setId(1001);
  5. person_student.setName("张三");
  6. person_student.setSex(true);
  7. person_student.setAddress("北京");
  8. person_student.setAge(20);
  9. print(person_student);
  10. Person person_emp = new Employee();
  11. person_emp.setId(1002);
  12. person_emp.setName("李四");
  13. person_emp.setSex(true);
  14. person_emp.setAddress("上海");
  15. person_emp.setAge(30);
  16. print(person_emp);
  17. Person person = new Person();
  18. person.setId(1003);
  19. person.setName("王五");
  20. person.setSex(true);
  21. print(person);
  22. }
  23. private static void print(Person person) {
  24. person.printInfo();
  25. }
  26. }
  27. class Person {
  28. //学号
  29. private int id;
  30. //姓名
  31. private String name;
  32. //性别
  33. private boolean sex;
  34. //地址
  35. private String address;
  36. //年龄
  37. private int age;
  38. public void printInfo() {
  39. System.out.println("id=" + id + ", name=" + name + ",sex=" + sex + ", address=" + address + ", age=" + age);
  40. }
  41. //设置学号
  42. public void setId(int studentId) {
  43. id = studentId;
  44. }
  45. //读取学号
  46. public int getId() {
  47. return id;
  48. }
  49. public void setName(String studentName) {
  50. name = studentName;
  51. }
  52. public String getName() {
  53. return name;
  54. }
  55. public void setSex(boolean studentSex) {
  56. sex = studentSex;
  57. }
  58. public boolean getSex() {
  59. return sex;
  60. }
  61. public void setAddress(String studentAddress) {
  62. address = studentAddress;
  63. }
  64. public String getAddress() {
  65. return address;
  66. }
  67. public void setAge(int studentAge) {
  68. if (studentAge >=0 && studentAge <=120) {
  69. age = studentAge;
  70. }
  71. }
  72. public int getAge() {
  73. return age;
  74. }
  75. }
  76. class Student extends Person {
  77. //班级编号
  78. private int classesId;
  79. public void setClassesId(int classesId) {
  80. this.classesId = classesId;
  81. }
  82. public int getClassesId() {
  83. return classesId;
  84. }
  85. public void printInfo() {
  86. System.out.println("id=" + getId() + ", name=" + getName() + ",sex=" + getSex() + ", address=" + getAddress() + ", age=" + getAge() + ",classesid=" + classesId);
  87. }
  88. }
  89. class Employee extends Person {
  90. //工作年限
  91. private int workYear;
  92. public void setWorkYear(int workYear) {
  93. this.workYear = workYear;
  94. }
  95. public int getWorkYear() {
  96. return workYear;
  97. }
  98. public void printInfo() {
  99. System.out.println("id=" + getId() + ", name=" + getName() + ",sex=" + getSex() + ", address=" + getAddress() + ", age=" + getAge() + ", workYear=" + workYear);
  100. }
  101. }

多态其实就是多种状态,overload(重载)是多态的一种,属于编译期绑定,也就是静态绑定(前期绑定),override是运行期间绑定(后期绑定)。
多态存在的条件:

  • 有继承
  • 有覆盖
  • 父类指向子类的引用

1646728510(1).png

2、对静态方法覆盖

【代码示例】、

  1. public class OverrideTest05 {
  2. public static void main(String[] args) {
  3. Person person_student = new Student();
  4. person_student.setId(1001);
  5. person_student.setName("张三");
  6. person_student.setSex(true);
  7. person_student.setAddress("北京");
  8. person_student.setAge(20);
  9. print(person_student);
  10. Person person_emp = new Employee();
  11. person_emp.setId(1002);
  12. person_emp.setName("李四");
  13. person_emp.setSex(true);
  14. person_emp.setAddress("上海");
  15. person_emp.setAge(30);
  16. print(person_emp);
  17. }
  18. private static void print(Person person) {
  19. person.printInfo();
  20. }
  21. }
  22. class Person {
  23. //学号
  24. private int id;
  25. //姓名
  26. private String name;
  27. //性别
  28. private boolean sex;
  29. //地址
  30. private String address;
  31. //年龄
  32. private int age;
  33. public static void printInfo() {
  34. System.out.println("------------Person--------------");
  35. }
  36. //设置学号
  37. public void setId(int studentId) {
  38. id = studentId;
  39. }
  40. //读取学号
  41. public int getId() {
  42. return id;
  43. }
  44. public void setName(String studentName) {
  45. name = studentName;
  46. }
  47. public String getName() {
  48. return name;
  49. }
  50. public void setSex(boolean studentSex) {
  51. sex = studentSex;
  52. }
  53. public boolean getSex() {
  54. return sex;
  55. }
  56. public void setAddress(String studentAddress) {
  57. address = studentAddress;
  58. }
  59. public String getAddress() {
  60. return address;
  61. }
  62. public void setAge(int studentAge) {
  63. if (studentAge >=0 && studentAge <=120) {
  64. age = studentAge;
  65. }
  66. }
  67. public int getAge() {
  68. return age;
  69. }
  70. }
  71. class Student extends Person {
  72. //班级编号
  73. private int classesId;
  74. public void setClassesId(int classesId) {
  75. this.classesId = classesId;
  76. }
  77. public int getClassesId() {
  78. return classesId;
  79. }
  80. public static void printInfo() {
  81. System.out.println("------------Student------------");
  82. }
  83. }
  84. class Employee extends Person {
  85. //工作年限
  86. private int workYear;
  87. public void setWorkYear(int workYear) {
  88. this.workYear = workYear;
  89. }
  90. public int getWorkYear() {
  91. return workYear;
  92. }
  93. public static void printInfo() {
  94. System.out.println("------------Employee------------");
  95. }
  96. }

从上面的输出可以看到,静态方法不存在多态的概念,多态和成员方法相关,静态方法只属于某一个类,声明的是那个一个类就调用的是哪一个类的静态方法和子类没有关系,不像覆盖了成员方法,new谁调谁,为什么覆盖成员方法可以构成多态(多种状态),主要是运行期的动态绑定(动态绑定构成多态),静态绑定的含义是在编译成class文件(字节码)的时候已经确定该调用哪个方法。

4、 super关键字

super关键字的作用:

  • 调用父类的构造方法
  • 调用父类的成员方法

需要注意:super只能应用在成员方法和构造方法中,不能应用在静态方法中(和this是一样的,代表父类对象),如果在构造方法中使用必须放在第一行
为什么会有super关键字?

  • 因为子类必须要调用父类的构造方法,先把父类构造完成,因为子类依赖于父类,没有父,也就没有子
  • 有时需要在子类中显示的调用父类的成员方法

那么我们以前为什么没有看到super,而且我们也有继承,如:Student继承了Person?

  • 因为子类中我们没有显示的调用构造方法,那么他会默认调用父类的无参构造方法,此种情况下如果父类中没有无参构造方法,那么编译时将会失败

注意构造方法不存在覆盖的概念,构造方法可以重载

1、调用默认构造方法

  1. public class SuperTest01 {
  2. public static void main(String[] args) {
  3. Person student = new Student();
  4. }
  5. }
  6. class Person {
  7. //学号
  8. private int id;
  9. //姓名
  10. private String name;
  11. //性别
  12. private boolean sex;
  13. //地址
  14. private String address;
  15. //年龄
  16. private int age;
  17. public Person() {
  18. //System.out.println(this);
  19. System.out.println("----------Person-----------");
  20. }
  21. //设置学号
  22. public void setId(int studentId) {
  23. id = studentId;
  24. }
  25. //读取学号
  26. public int getId() {
  27. return id;
  28. }
  29. public void setName(String studentName) {
  30. name = studentName;
  31. }
  32. public String getName() {
  33. return name;
  34. }
  35. public void setSex(boolean studentSex) {
  36. sex = studentSex;
  37. }
  38. public boolean getSex() {
  39. return sex;
  40. }
  41. public void setAddress(String studentAddress) {
  42. address = studentAddress;
  43. }
  44. public String getAddress() {
  45. return address;
  46. }
  47. public void setAge(int studentAge) {
  48. if (studentAge >=0 && studentAge <=120) {
  49. age = studentAge;
  50. }
  51. }
  52. public int getAge() {
  53. return age;
  54. }
  55. }
  56. class Student extends Person {
  57. //班级编号
  58. private int classesId;
  59. public Student() {
  60. //System.out.println(this);
  61. //显示调用父类的构造方法
  62. //调用父类的无参的构造函数
  63. //super();
  64. System.out.println("--------Student----------");
  65. //编译错误,必须将super放到构造函数的第一语句
  66. //必须先创建父类,才能创建子类
  67. //super();
  68. }
  69. public void setClassesId(int classesId) {
  70. this.classesId = classesId;
  71. }
  72. public int getClassesId() {
  73. return classesId;
  74. }
  75. }

关于构造方法的执行顺序,先执行父类的再执行子类的,必须完成父类的所有初始化,才能创建子类

2、调用带参数的构造方法

  1. public class SuperTest02 {
  2. public static void main(String[] args) {
  3. /*
  4. Person person_student = new Student();
  5. person_student.setId(1001);
  6. person_student.setName("张三");
  7. person_student.setSex(true);
  8. person_student.setAddress("北京");
  9. person_student.setAge(20);
  10. */
  11. //编译出错,因为Person看不到子类Student的属性
  12. //person_student.setClassesId(10);
  13. /*
  14. person_student.printInfo();
  15. System.out.println("");
  16. Person person_emp = new Employee();
  17. person_emp.setId(1002);
  18. person_emp.setName("李四");
  19. person_emp.setSex(true);
  20. person_emp.setAddress("上海");
  21. person_emp.setAge(30);
  22. */
  23. //编译出错,因为Person看不到子类Employee的属性
  24. //person_emp.setWorkYear(10);
  25. //person_emp.printInfo();
  26. Person person_student = new Student(1001, "张三", true, "北京", 20, 10);
  27. person_student.printInfo();
  28. }
  29. }
  30. class Person {
  31. //学号
  32. private int id;
  33. //姓名
  34. private String name;
  35. //性别
  36. private boolean sex;
  37. //地址
  38. private String address;
  39. //年龄
  40. private int age;
  41. public Person() {
  42. System.out.println("--------------Person-------------");
  43. }
  44. public Person(int id, String name, boolean sex, String address, int age) {
  45. this.id = id;
  46. this.name = name;
  47. this.sex = sex;
  48. this.address = address;
  49. this.age= age;
  50. }
  51. public void printInfo() {
  52. System.out.println("id=" + id + ", name=" + name + ",sex=" + sex + ", address=" + address + ", age=" + age);
  53. }
  54. //设置学号
  55. public void setId(int studentId) {
  56. id = studentId;
  57. }
  58. //读取学号
  59. public int getId() {
  60. return id;
  61. }
  62. public void setName(String studentName) {
  63. name = studentName;
  64. }
  65. public String getName() {
  66. return name;
  67. }
  68. public void setSex(boolean studentSex) {
  69. sex = studentSex;
  70. }
  71. public boolean getSex() {
  72. return sex;
  73. }
  74. public void setAddress(String studentAddress) {
  75. address = studentAddress;
  76. }
  77. public String getAddress() {
  78. return address;
  79. }
  80. public void setAge(int studentAge) {
  81. if (studentAge >=0 && studentAge <=120) {
  82. age = studentAge;
  83. }
  84. }
  85. public int getAge() {
  86. return age;
  87. }
  88. }
  89. class Student extends Person {
  90. //班级编号
  91. private int classesId;
  92. public Student(int id, String name, boolean sex, String address, int age, int classesId) {
  93. /*
  94. this.id = id;
  95. this.name = name;
  96. this.sex = sex;
  97. this.address = address;
  98. this.age= age;
  99. this.classesId = id;
  100. */
  101. /*
  102. setId(id);
  103. setName(name);
  104. setSex(sex);
  105. setAddress(address);
  106. setAge(age);
  107. this.classesId = classesId;
  108. */
  109. //手动调用调用带参数的构造函数
  110. super(id, name, sex, address, age);
  111. this.classesId = classesId;
  112. }
  113. public void setClassesId(int classesId) {
  114. this.classesId = classesId;
  115. }
  116. public int getClassesId() {
  117. return classesId;
  118. }
  119. public void printInfo() {
  120. System.out.println("id=" + getId() + ", name=" + getName() + ",sex=" + getSex() + ", address=" + getAddress() + ", age=" + getAge() + ",classesid=" + classesId);
  121. }
  122. }
  123. class Employee extends Person {
  124. //工作年限
  125. private int workYear;
  126. public void setWorkYear(int workYear) {
  127. this.workYear = workYear;
  128. }
  129. public int getWorkYear() {
  130. return workYear;
  131. }
  132. public void printInfo() {
  133. System.out.println("id=" + getId() + ", name=" + getName() + ",sex=" + getSex() + ", address=" + getAddress() + ", age=" + getAge() + ", workYear=" + workYear);
  134. }
  135. }

3、采用super调用父类的方法

  1. public class SuperTest03 {
  2. public static void main(String[] args) {
  3. Person person_student = new Student();
  4. person_student.setId(1001);
  5. person_student.setName("张三");
  6. person_student.setSex(true);
  7. person_student.setAddress("北京");
  8. person_student.setAge(20);
  9. print(person_student);
  10. Person person_emp = new Employee();
  11. person_emp.setId(1002);
  12. person_emp.setName("李四");
  13. person_emp.setSex(true);
  14. person_emp.setAddress("上海");
  15. person_emp.setAge(30);
  16. print(person_emp);
  17. }
  18. private static void print(Person person) {
  19. person.printInfo();
  20. }
  21. }
  22. class Person {
  23. //学号
  24. private int id;
  25. //姓名
  26. private String name;
  27. //性别
  28. private boolean sex;
  29. //地址
  30. private String address;
  31. //年龄
  32. private int age;
  33. public void printInfo() {
  34. System.out.println("id=" + id + ", name=" + name + ",sex=" + sex + ", address=" + address + ", age=" + age);
  35. }
  36. //设置学号
  37. public void setId(int studentId) {
  38. id = studentId;
  39. }
  40. //读取学号
  41. public int getId() {
  42. return id;
  43. }
  44. public void setName(String studentName) {
  45. name = studentName;
  46. }
  47. public String getName() {
  48. return name;
  49. }
  50. public void setSex(boolean studentSex) {
  51. sex = studentSex;
  52. }
  53. public boolean getSex() {
  54. return sex;
  55. }
  56. public void setAddress(String studentAddress) {
  57. address = studentAddress;
  58. }
  59. public String getAddress() {
  60. return address;
  61. }
  62. public void setAge(int studentAge) {
  63. if (studentAge >=0 && studentAge <=120) {
  64. age = studentAge;
  65. }
  66. }
  67. public int getAge() {
  68. return age;
  69. }
  70. }
  71. class Student extends Person {
  72. //班级编号
  73. private int classesId;
  74. public void setClassesId(int classesId) {
  75. this.classesId = classesId;
  76. }
  77. public int getClassesId() {
  78. return classesId;
  79. }
  80. public void printInfo() {
  81. //System.out.println("id=" + getId() + ", name=" + getName() + ",sex=" + getSex() + ", address=" + getAddress() + ", age=" + getAge() + ",classesid=" + classesId);
  82. //采用super调用父类的方法
  83. super.printInfo();
  84. System.out.println("classesId=" + classesId);
  85. }
  86. }
  87. class Employee extends Person {
  88. //工作年限
  89. private int workYear;
  90. public void setWorkYear(int workYear) {
  91. this.workYear = workYear;
  92. }
  93. public int getWorkYear() {
  94. return workYear;
  95. }
  96. public void printInfo() {
  97. //System.out.println("id=" + getId() + ", name=" + getName() + ",sex=" + getSex() + ", address=" + getAddress() + ", age=" + getAge() + ", workYear=" + workYear);
  98. System.out.println("workYear=" + workYear);
  99. //采用super调用父类的方法
  100. super.printInfo();
  101. }
  102. }

5、 final关键字

final表示不可改变的含义

  • 采用final修饰的类不能被继承
  • 采用final修饰的方法不能被覆盖
  • 采用final修饰的变量不能被修改
  • final修饰的变量必须显示初始化
  • 如果修饰的引用,那么这个引用只能指向一个对象,也就是说这个引用不能再次赋值,但被指向的对象是可以修改的
  • 构造方法不能被final修饰
  • 会影响JAVA类的初始化:final定义的静态常量调用时不会执行java的类初始化方法,也就是说不会执行static代码块等相关语句,这是由java虚拟机规定的。我们不需要了解的很深,有个概念就可以了。

    1、采用final修饰的类不能被继承

    ```java public class FinalTest01 {

    public static void main(String[] args) {

    }
    }

final class A1 { public void test1() {

  1. }

}

//不能继承A1,因为A1采用final修饰了 class B1 extends A1 {

  1. public void test2() {
  2. }

}

  1. <a name="poRfD"></a>
  2. ### **2、采用final修饰的方法不能被覆盖**
  3. ```java
  4. public class FinalTest02 {
  5. public static void main(String[] args) {
  6. }
  7. }
  8. class A1 {
  9. public final void test1() {
  10. }
  11. }
  12. class B1 extends A1 {
  13. //覆盖父类的方法,改变其行为
  14. //因为父类的方法是final修饰的,所以不能覆盖
  15. public void test1() {
  16. }
  17. public void test2() {
  18. }
  19. }

3、采用final修饰的变量(基本类型)不能被修改

  1. public class FinalTest03 {
  2. private static final long CARD_NO = 878778878787878L;
  3. public static void main(String[] args) {
  4. //不能进行修改,因为CARD_NO采用final修改了
  5. CARD_NO = 99999999999999L;
  6. }
  7. }

4、final修饰的变量必须显示初始化

  1. public class FinalTest04 {
  2. //如果是final修饰的变量必须初始化
  3. private static final long CARD_NO = 0L;
  4. public static void main(String[] args) {
  5. int i;
  6. //局部变量必须初始化
  7. //如果不使用可以不初始化
  8. System.out.println(i);
  9. }
  10. }

5、如果修饰的引用,那么这个引用只能指向一个对象,也就是说这个引用不能再次赋值,但被指向的对象是可以修改的

  1. public class FinalTest05 {
  2. public static void main(String[] args) {
  3. Person p1 = new Person();
  4. //可以赋值
  5. p1.name = "张三";
  6. System.out.println(p1.name);
  7. final Person p2 = new Person();
  8. p2.name = "李四";
  9. System.out.println(p2.name);
  10. //不能编译通过
  11. //p2采用final修饰,主要限制了p2指向堆区中的地址不能修改(也就是p2只能指向一个对象)
  12. //p2指向的对象的属性是可以修改的
  13. p2 = new Person();
  14. }
  15. }
  16. class Person {
  17. String name;
  18. }

1646729170(1).png