• 【Java初阶】面向对象三大特性之继承


     🎈🎈作者 :whispar
    🎈🎈专栏 :小题练手

    🌈刷题,面试,求职,快来牛客网一起成为offer收割机!🌈

    点击注册收割offer

    d924065539c14401af169e0db320941a.png


    目录

    一、继承

    二、super关键字

    三、继承关系下的代码块

    四、protected关键字

    五、final关键字


    一、继承

    • 理解继承

    Cat继承了Animal类,其中:Animal类称为父类/基类或超类,Cat可以称为Animal的 子类/派生类,继承之后,子类可以复用父类中成员,子类在实现时只需关心自己新增加的成员即可

    Java中不支持多继承,仅支持A extends B 单继承//A extends B B extends C 多层继承 //A extends C B extends C 不同类继承自同一类。

    1. public class TestExtend {
    2.    public static void main(String[] args) {
    3.       Dog d1 =new Dog();
    4.       d1.name = "xiaocs";
    5.       d1.age = 15;
    6.       Cat c1 = new Cat();
    7.       c1.color = "red";
    8.       c1.type = "波斯";
    9.       c1.friendly();
    10.   }
    11. }
    12. /**
    13. *   Dog未继承animal则无法调用animal中的成员变量和成员方法
    14. */
    15. class Dog{
    16.    public  int age;
    17.    public String  name;
    18.    
    19.    public void shot(){
    20.        System.out.println("汪汪汪");
    21.   }
    22. }
    23. //cat可以直接调用animal中的成员变量和成员方法
    24. class Cat extends animal{
    25.    public int weight;
    26.    public String sex;
    27.    
    28.    public void eat(){
    29.        System.out.println("嘎嘎吃");
    30.   }
    31. }
    32. class animal{
    33.    public String  color;
    34.    public String type;
    35.    
    36.    public void friendly(){
    37.        System.out.println("对人类友好");
    38.   }
    39. }
    • 子类访问父类

      • 子类和父类不存在同名成员变量

        1. public class Base {
        2.   int a;
        3.   int b;
        4. }
        5. public class Derived extends Base{
        6.   int c;
        7.   public void method(){
        8.       a = 10; // 访问从父类中继承下来的a
        9.       b = 20; // 访问从父类中继承下来的b
        10.       c = 30; // 访问子类自己的c
        11.   }

         

      • 子类和父类成员变量同名

        1. public class Base {
        2.    int a;
        3.    int b;
        4.    int c;
        5. }
        6. public class Derived extends Base{
        7.    int a; // 与父类中成员a同名,且类型相同
        8.    char b; // 与父类中成员b同名,但类型不同
        9.    public void method(){
        10.        a = 100;
        11.        b = 101;
        12.        c = 102; // 子类没有c,访问的肯定是从父类继承下来的c
        13.     // d = 103; // 编译失败,因为父类和子类都没有定义成员变量b
        14.   }
        15. }
        • 成员变量访问遵循就近原则,自己有优先自己的,与父类同名也优先访问自己的,如果没有则向父类中找。

        • 成员方法亦然,如果子类有则先访问子类的,子类没有先访问父类的,如果存在方法重载,则根据引用的情况,选择合适的方法

    • 在子类方法中,如果想要明确访问父类中成员时,借助super关键字即可

    二、super关键字

    • 当父类和子类的成员变量或成员方法相同时,调用父类的成员变量或方法,使用super关键字

    1. class Cat extends animal{
    2.   public int color;
    3.   public String type;
    4.   public void eat(){
    5.    
    6.   //调用父类的成员变量
    7.       super.color = "red";
    8.       super.type = "波斯";
    9.       super.friendly();
    10.       System.out.println("嘎嘎吃");
    11.   }
    12. }
    13. class animal{
    14.   public String color;
    15.   public String type;
    16.   public void friendly(){
    17.       System.out.println("对人类友好");
    18.   }
    19. }
    • 子类的构造方法(无参数)

    • 若父类显式定义无参或者默认的构造方法,在子类构造方法第一行默认有隐含的super()调用,即调用基类构造方法

    1. public class TestExtend {
    2.    public static void main(String[] args) {
    3.        Cat c = new Cat();
    4.   }
    5. }
    6. class Cat extends animal{
    7.    public int size;
    8.    public String name;
    9.    public Cat(){
    10.        // super();
    11.                   //注意子类构造方法中默认会调用基类的无参构造方法:super(),
    12.                   // 用户没有写时,编译器会自动添加,而且super()必须是子类构造方法中第一条语句,
    13.                   // 并且只能出现一次
    14.        System.out.println("Cat::");
    15.   }
    16.    public void eat(){
    17.        System.out.println("嘎嘎吃");
    18.   }
    19. }
    20. class animal{
    21.    public String  color;
    22.    public String type;
    23.    public animal() {
    24.        System.out.println("animal::");
    25.   }
    26.    public void friendly(){
    27.        System.out.println("对人类友好");
    28.   }
    29. }

    795895d35f43fa9553576f9c19182fab.png

    子类对象构造时,需要先调用父类构造方法,然后执行子类的构造方法。

    • 子类的构造方法(有参数)

    • 如果父类构造方法是带有参数的,此时需要用户为子类显式定义构造方法,并在子类构造方法中选择合适的父类构造方法调用,否则会编译失败

    1. /**
    2. * Created with IntelliJ IDEA.
    3. * Description:
    4. * User: snutyx
    5. * Date: 2022-08-30
    6. * Time: 17:05
    7. */
    8. public class TestExtend {
    9.   public static void main(String[] args) {
    10.       Cat c = new Cat("red","波斯",45,"xiaos");
    11.   }
    12. }
    13. class Cat extends animal{
    14.   public int size;
    15.   public String name;
    16.   public Cat(String color, String type, int size, String name) {
    17.       super(color, type);
    18.       this.size = size;
    19.       this.name = name;
    20.   }
    21.   public void eat(){
    22.       System.out.println("嘎嘎吃");
    23.   }
    24. }
    25. class animal{
    26.   public String color;
    27.   public String type;
    28.   public animal(String color, String type) {
    29.       this.color = color;
    30.       this.type = type;
    31.   }
    32.   public void friendly(){
    33.       System.out.println("对人类友好");
    34.   }
    35. }
    • 在子类构造方法中,super(...)调用父类构造时,必须是子类构造函数中第一条语句。

    • super(...)只能在子类构造方法中出现一次,并且不能和this同时出现

    ✅super和this之间的区别?

    • 相同点:

    1.都属于java 中 的关键字

    2.只能在类的非静态方法中使用,用来访问 非静态的方法和字段

    3.在构造 方法中使用时必须是构造方法当中的第一条语句,并且二者不能同时存在

    • 不同点:

      1.this指的是当前对象的引用,super指的是当前对象对父类部分对象的使用

    1. 在非静态成员方法中,this用来访问本类的方法和属性,super用来访问父类继承下来的方法和属性

    2. 在构造方法当中,this用于调用本类的构造方法,super调用父类的构造方法,两种调用不能同时在构造方法中出现

    3. 构造方法中一定会存在super(...)的调用,用户没有写编译器也会增加,但是this(...)用户不写则没有

    三、继承关系下的代码块

    1. public class TestExtend {
    2.    public static void main(String[] args) {
    3.        Cat c = new Cat("red","波斯",45,"xiaos");
    4.   }
    5. }
    6. class Cat extends animal{
    7.    public int size;
    8.    public String name;
    9.    //5.子类的实例代码块
    10.   {
    11.        System.out.println("cat::实例");
    12.   }
    13.    //2.子类的静态代码块
    14.    static{
    15.        System.out.println("cat::static");
    16.   }
    17.    //6.子类的构造方法
    18.    public Cat(String color, String type, int size, String name) {
    19.        super(color, type);
    20.        this.size = size;
    21.        this.name = name;
    22.        System.out.println("cat::构造");
    23.   }
    24.    public void eat(){
    25.        System.out.println("嘎嘎吃");
    26.   }
    27. }
    28. class animal{
    29.    public String  color;
    30.    public String type;
    31.    //3.父类的实例代码块
    32.   {
    33.        System.out.println("animal::实例");
    34.   }
    35.    //1。父类的静态代码块
    36.    static{
    37.        System.out.println("animal::static");
    38.   }
    39.    //4.父类的构造方法
    40.    public animal(String color, String type) {
    41.        this.color = color;
    42.        this.type = type;
    43.        System.out.println("animal::构造");
    44.   }
    45.    public void friendly(){
    46.        System.out.println("对人类友好");
    47.   }

    bd42ab61ac12c14afe62ef932db7b72e.png

    根据执行结果可以的出以下结论:

    父类的静态代码块>>子类的静态代码块>>父类的实例代码块>父类的构造方法>>子类的实例代码块>> 子类的构造方法

    • 第二次实例化子类对象时,父类和子类的静态代码块都将不会再执行

    四、protected关键字

    访问限定:同一个包中的类和不同包中的子类

    1. package TestDemo2;
    2. public class B {
    3.    private int a;
    4.    protected int b;
    5.    public int c;
    6.    int d;
    7. }

    在不同包中即package TestDemo 中

    104e07fbff4c2b03429040c08973b5e7.png

    只有public和protected修饰的成员可以访问

    五、final关键字

    • 修饰变量时: 表示常量即不能修改

    1. final int a = 10;
    2. a = 20; // 编译出错
    • 修饰类时: 表示此类不能被继承,即不能作为父类

    1. //final修饰
    2. final class animal{
    3. public String color;
    4. public String type;
    5. public animal(String color, String type) {
    6. this.color = color;
    7. this.type = type;
    8. System.out.println("animal::构造");
    9. }
    10. }

    c069e454132c34b75586807d896fcbb9.png

     

     

  • 相关阅读:
    AtCoder Beginner Contest 228(A-Ex)
    Spring Framework 学习笔记3:整合 MyBatis+JUnit
    golang笔记 mutex,抢占式调度,semaphore
    NLP算法面经 | 腾讯 VS 美团
    Windows XP的特点和运行环境分别是什么
    vue3 第二天vue响应式原理以及ref和reactive区别
    串行、并行、并发
    pygame实现时钟
    AI项目十五:PP-Humanseg训练及onnxruntime部署
    vue3代码检查以及格式化配置
  • 原文地址:https://blog.csdn.net/m0_56361048/article/details/126652907